Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Regular expressions question

Reply
Thread Tools

Regular expressions question

 
 
Victor Polukcht
Guest
Posts: n/a
 
      01-16-2007
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?

 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      01-16-2007
"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?
>

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?
 
Reply With Quote
 
 
 
 
Victor Polukcht
Guest
Posts: n/a
 
      01-16-2007
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.invalid> wrote:
> "Victor Polukcht" <vpoluk...@gmail.com> 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?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?


 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      01-16-2007
On 2007-01-16, Victor Polukcht <> wrote:
> 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
 
Reply With Quote
 
Victor Polukcht
Guest
Posts: n/a
 
      01-16-2007
The same regular expression should work for another string (with *200).

On Jan 16, 5:40 pm, Neil Cerutti <horp...@yahoo.com> wrote:
> On 2007-01-16, Victor Polukcht <vpoluk...@gmail.com> wrote:
>
> > 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


 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      01-16-2007
On 2007-01-16, Victor Polukcht <> wrote:
> On Jan 16, 5:40 pm, Neil Cerutti <horp...@yahoo.com> wrote:
>> On 2007-01-16, Victor Polukcht <vpoluk...@gmail.com> wrote:
>>
>> > 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
 
Reply With Quote
 
Wolfgang Grafen
Guest
Posts: n/a
 
      01-16-2007
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')
 
Reply With Quote
 
Jussi Salmela
Guest
Posts: n/a
 
      01-16-2007
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
 
Reply With Quote
 
Victor Polukcht
Guest
Posts: n/a
 
      01-16-2007
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')


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions Question. questionmarc420@msn.com Java 9 01-23-2006 10:32 PM
Question on regular expressions ASP.Confused ASP .Net 2 07-26-2004 04:14 PM
XML schema regular expressions question and recommended XML Schema book Fred Smith XML 1 02-05-2004 11:12 AM
Another Regular Expressions question jeffM Java 3 02-02-2004 11:03 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57