440,551 Members | 1,127 Online
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

# Regular expressions question

 P: n/a 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 Replies

 P: n/a "Victor Polukcht"

 P: n/a 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

 P: n/a On 2007-01-16, Victor Polukcht >g = "Global etsi3 * 4 ok 30 100% 100% Outgoing"import rer = 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

 P: n/a The same regular expression should work for another string (with *200). On Jan 16, 5:40 pm, Neil Cerutti g = "Global etsi3 * 4 ok 30 100% 100% Outgoing"import rer = 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

 P: n/a On 2007-01-16, Victor Polukcht On 2007-01-16, Victor Polukcht >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

 P: n/a 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

 P: n/a 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

 P: n/a Great thnx. It works. On Jan 16, 6:02 pm, Wolfgang Grafen 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 discussion thread is closed

Replies have been disabled for this discussion.