Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11880
| Date | 2011-08-19 21:43 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Help with regular expression in python |
| References | <201108181349.54727.matze999@gmail.com> <mailman.222.1313767221.27778.python-list@python.org> <87hb5d4cik.fsf@dpt-info.u-strasbg.fr> <mailman.227.1313775252.27778.python-list@python.org> <58c66ba7-daf6-4e98-ba26-e054d9fde5f0@z17g2000vbp.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.238.1313786638.27778.python-list@python.org> (permalink) |
On 19/08/2011 20:55, rurpy@yahoo.com wrote:
> On 08/19/2011 11:33 AM, Matt Funk wrote:
>> On Friday, August 19, 2011, Alain Ketterlin wrote:
>>> Matt Funk<matze999@gmail.com> writes:
>>>> thanks for the suggestion. I guess i had found another way around the
>>>> problem as well. But i really wanted to match the line exactly and i
>>>> wanted to know why it doesn't work. That is less for the purpose of
>>>> getting the thing to work but more because it greatly annoys me off that
>>>> i can't figure out why it doesn't work. I.e. why the expression is not
>>>> matches {32} times. I just don't get it.
>>>
>>> Because a line is not 32 times a number, it is a number followed by 31
>>> times "a space followed by a number". Using Jason's regexp, you can
>>> build the regexp step by step:
>>>
>>> number = r"\d\.\d+e\+\d+"
>>> numbersequence = r"%s( %s){31}" % (number,number)
>> That didn't work either. Using the (modified (where the (.+) matches the end of
>> the line)) expression as:
>>
>> number = r"\d\.\d+e\+\d+"
>> numbersequence = r"%s( %s){31}(.+)" % (number,number)
>> instance_linetype_pattern = re.compile(numbersequence)
>>
>> The results obtained are:
>> results:
>> [(' 2.199000e+01', ' : (instance: 0)\t:\tsome description')]
>> so this matches the last number plus the string at the end of the line, but no
>> retaining the previous numbers.
>
> The secret is buried very unobtrusively in the re docs,
> where it has caught me out in the past. Specifically
> in the docs for re.group():
>
> "If a group is contained in a part of the pattern that
> matched multiple times, the last match is returned."
>
[snip]
There's a regex implementation on PyPI:
http://pypi.python.org/pypi/regex
which does support capturing all of the matches of a group.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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