473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regular expressions question

I have 2 strings:

"Global etsi3 *200 ok 30 100% 100%
Outgoing"
and
"Global etsi3 * 4 ok 30 100% 100%
Outgoing"

The difference is "*200" instead of "* 4". Is there ability to write a
regular expression that will match both of that strings?

Jan 16 '07 #1
8 1026
"Victor Polukcht" <vp*******@gmail.comwrote:
I have 2 strings:

"Global etsi3 *200 ok 30 100% 100%
Outgoing"
and
"Global etsi3 * 4 ok 30 100% 100%
Outgoing"

The difference is "*200" instead of "* 4". Is there ability to write a
regular expression that will match both of that strings?
Yes, ".*" would match both of the strings, but not in a useful way. You'll
have to consider which strings you *don't* want to match as well as which
ones you do and whether you want to extract any information from the
strings or find the ones which match.

But first take a step back and look at the problem as a whole. You didn't
say what you are trying to do, and often people will jump at regular
expressions as the solution when there may be better ways of doing what
they want without writing a regular expression.

What do you really want to do?
Jan 16 '07 #2
Actually, i'm trying to get the values of first field (Global) , fourth
(200, 4), and fifth (100%) and sixth (100%).

Everything except fourth is simple.

On Jan 16, 2:59 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
"Victor Polukcht" <vpoluk...@gmail.comwrote:
I have 2 strings:
"Global etsi3 *200 ok 30 100% 100%
Outgoing"
and
"Global etsi3 * 4 ok 30 100% 100%
Outgoing"
The difference is "*200" instead of "* 4". Is there ability to write a
regular expression that will match both of that strings?Yes, ".*" would match both of the strings, but not in a useful way. You'll
have to consider which strings you *don't* want to match as well as which
ones you do and whether you want to extract any information from the
strings or find the ones which match.

But first take a step back and look at the problem as a whole. You didn't
say what you are trying to do, and often people will jump at regular
expressions as the solution when there may be better ways of doing what
they want without writing a regular expression.

What do you really want to do?
Jan 16 '07 #3
On 2007-01-16, Victor Polukcht <vp*******@gmail.comwrote:
Actually, i'm trying to get the values of first field (Global) , fourth
(200, 4), and fifth (100%) and sixth (100%).

Everything except fourth is simple.
>>g = "Global etsi3 * 4 ok 30 100% 100% Outgoing"
import re
r = re.search('\*\s+(\d+)', g)
r.group()
'* 4'
>>r.group(1)
'4'

--
Neil Cerutti
We're not afraid of challenges. It's like we always say: If you want to go out
in the rain, be prepared to get burned. --Brazillian soccer player
Jan 16 '07 #4
The same regular expression should work for another string (with *200).

On Jan 16, 5:40 pm, Neil Cerutti <horp...@yahoo.comwrote:
On 2007-01-16, Victor Polukcht <vpoluk...@gmail.comwrote:
Actually, i'm trying to get the values of first field (Global) , fourth
(200, 4), and fifth (100%) and sixth (100%).
Everything except fourth is simple.
>g = "Global etsi3 * 4 ok 30 100% 100% Outgoing"
import re
r = re.search('\*\s+(\d+)', g)
r.group()
'* 4'
>r.group(1)'4'

--
Neil Cerutti
We're not afraid of challenges. It's like we always say: If you want to go out
in the rain, be prepared to get burned. --Brazillian soccer player
Jan 16 '07 #5
On 2007-01-16, Victor Polukcht <vp*******@gmail.comwrote:
On Jan 16, 5:40 pm, Neil Cerutti <horp...@yahoo.comwrote:
>On 2007-01-16, Victor Polukcht <vpoluk...@gmail.comwrote:
Actually, i'm trying to get the values of first field (Global) , fourth
(200, 4), and fifth (100%) and sixth (100%).
Everything except fourth is simple.
g = "Global etsi3 * 4 ok 30 100% 100% Outgoing"
import re
r = re.search('\*\s+(\d+)', g)
r.group()
'* 4'
>>r.group(1)'4'

The same regular expression should work for another string (with *200).
Sorry about that. It should have been:

r = re.search('\*\s*(\d+)', g)

--
Neil Cerutti
Jan 16 '07 #6
Victor Polukcht wrote:
I have 2 strings:

"Global etsi3 *200 ok 30 100% 100%
Outgoing"
and
"Global etsi3 * 4 ok 30 100% 100%
Outgoing"

The difference is "*200" instead of "* 4". Is there ability to write a
regular expression that will match both of that strings?
---------------------------- x.py begin --------
import re

s1 = "Global etsi3 *200 ok 30 100% 100% Outgoing"
s2 = "Global etsi3 * 4 ok 30 100% 100% Outgoing"

re_m = re.compile( "^"
"(\S+)" # Global
"\s+"
"(\S+)" # etsi3
"\s+"
"((\*)\s*(\d+))" # *200 * 4
"\s+"
"(\S+)" # ok
"\s+"
"(\S+)" # 30
"\s+"
"(\S+)" # 100%
"\s+"
"(\S+)" # 100%
"\s+"
"(\S+)" # Outgoing
"$"
).match

print "match s1:", re_m(s1).groups()
print "match s2:", re_m(s2).groups()
----------------------------- x.py file end ---------

% python x.py
match s1: ('Global', 'etsi3', '*200', '*', '200', 'ok', '30', '100%', '100%', 'Outgoing')
match s2: ('Global', 'etsi3', '* 4', '*', '4', 'ok', '30', '100%', '100%', 'Outgoing')
Jan 16 '07 #7
Victor Polukcht kirjoitti:
I have 2 strings:

"Global etsi3 *200 ok 30 100% 100%
Outgoing"
and
"Global etsi3 * 4 ok 30 100% 100%
Outgoing"

The difference is "*200" instead of "* 4". Is there ability to write a
regular expression that will match both of that strings?
If the goal is not to study regular expressions, here's a solution
without them. Not so short, but working.

lst = [
"Global etsi3 *200 ok 30 100% 100%
Outgoing",
"Global etsi3 * 4 ok 30 100% 100%
Outgoing"]

for e in lst:
es = e.split()
if len(es) == 9:
num_val = es[3]
else:
num_val = es[2][1:]
print es[0], num_val, es[-3], es[-2]
Cheers,
Jussi
Jan 16 '07 #8
Great thnx. It works.

On Jan 16, 6:02 pm, Wolfgang Grafen <wolfgang.gra...@marconi.com>
wrote:
Victor Polukcht wrote:
I have 2 strings:
"Global etsi3 *200 ok 30 100% 100%
Outgoing"
and
"Global etsi3 * 4 ok 30 100% 100%
Outgoing"
The difference is "*200" instead of "* 4". Is there ability to write a
regular expression that will match both of that strings?---------------------------- x.py begin --------
import re

s1 = "Global etsi3 *200 ok 30 100% 100% Outgoing"
s2 = "Global etsi3 * 4 ok 30 100% 100% Outgoing"

re_m = re.compile( "^"
"(\S+)" # Global
"\s+"
"(\S+)" # etsi3
"\s+"
"((\*)\s*(\d+))" # *200 * 4
"\s+"
"(\S+)" # ok
"\s+"
"(\S+)" # 30
"\s+"
"(\S+)" # 100%
"\s+"
"(\S+)" # 100%
"\s+"
"(\S+)" # Outgoing
"$"
).match

print "match s1:", re_m(s1).groups()
print "match s2:", re_m(s2).groups()
----------------------------- x.py file end ---------

% python x.py
match s1: ('Global', 'etsi3', '*200', '*', '200', 'ok', '30', '100%', '100%', 'Outgoing')
match s2: ('Global', 'etsi3', '* 4', '*', '4', 'ok', '30', '100%', '100%', 'Outgoing')
Jan 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Toby | last post by:
Could some tell how I could create a search replace Regular Express in .net where is would match MY_STRING_TO_BE_CONVERTED and replace with MyStringToBeConverted
7
by: Patient Guy | last post by:
Coding patterns for regular expressions is completely unintuitive, as far as I can see. I have been trying to write script that produces an array of attribute components within an HTML element. ...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
12
by: =?Utf-8?B?SlA=?= | last post by:
I am a newbie to regular expressions and want to extract a number from the end of a string. The string would have these formats: image/4567 image/45678 image/456789 I would also want to...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
9
by: Rene | last post by:
I'm trying to basically remove chunks of html from a page but I must not be doing my regular expression correctly. What i'm trying with no avail. $site = preg_replace("/<!DOCTYPE(.|\s)*<div...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.