Regular expressions question

V

Victor Polukcht

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?
 
D

Duncan Booth

Victor Polukcht said:
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?
 
V

Victor Polukcht

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.
 
N

Neil Cerutti

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.
'4'
 
W

Wolfgang Grafen

Victor said:
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')
 
J

Jussi Salmela

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
 
V

Victor Polukcht

Great thnx. It works.

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')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top