Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12028

Re: Help with regular expression in python

References <201108181349.54727.matze999@gmail.com> <mailman.227.1313775252.27778.python-list@python.org> <c83c4dd3-7451-4c55-81d5-ae9c575381b1@glegroupsg2000goo.googlegroups.com> <201108191555.53338.matze999@gmail.com>
Date 2011-08-22 15:59 +0200
Subject Re: Help with regular expression in python
From Vlastimil Brom <vlastimil.brom@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.317.1314021558.27778.python-list@python.org> (permalink)

Show all headers | View raw


Sorry, if I missed some further specification in the earlier thread or
if the following is oversimplification of the original problem (using
3 numbers instead of 32),
would something like the following work for your data?

>>> import re
>>> data = """2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0)       :       some description
... 2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0)       :
  some description
... 2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0)       :
  some description
... 2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0)       :
  some description"""
>>> for res in re.findall(r"(?m)^(?:(?:[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+))?\s+){3}(?:.+)$", data): print res
...
2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0)       :
some description
2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0)       :
some description
2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0)       :
some description
2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0)       :
some description
>>>

i.e. all parentheses are non-capturing (?:...) and there are extra
anchors for line begining and end ^...$ with the multiline flag set
via (?m)
Each result is one matching line in this sample (if you need to acces
single numbers, you could process these matches further or use the new
regex implementation mentioned earlier by mrab (its developer) with
the new match method captures() - using an appropriate pattern with
the needed groupings).

regards,
  vbr

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Help with regular expression in python Matt Funk <matze999@gmail.com> - 2011-08-19 09:20 -0600
  Re: Help with regular expression in python jmfauth <wxjmfauth@gmail.com> - 2011-08-19 08:50 -0700
  Re: Help with regular expression in python Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-19 18:00 +0200
    Re: Help with regular expression in python Matt Funk <matze999@gmail.com> - 2011-08-19 11:33 -0600
      Re: Help with regular expression in python jmfauth <wxjmfauth@gmail.com> - 2011-08-19 11:40 -0700
        Re: Help with regular expression in python Matt Funk <matze999@gmail.com> - 2011-08-19 15:21 -0600
      Re: Help with regular expression in python "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-08-19 12:55 -0700
        Re: Help with regular expression in python MRAB <python@mrabarnett.plus.com> - 2011-08-19 21:43 +0100
      Re: Help with regular expression in python Carl Banks <pavlovevidence@gmail.com> - 2011-08-19 13:11 -0700
        Re: Help with regular expression in python Matt Funk <matze999@gmail.com> - 2011-08-19 15:55 -0600
        Re: Help with regular expression in python Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-08-22 15:59 +0200

csiph-web