Regular expressions question

Wolfgang Grafen wolfgang.grafen at marconi.com
Tue Jan 16 10:02:36 EST 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')



More information about the Python-list mailing list