[Pythonmac-SIG] a RegEx question
Charles Hartman
charles.hartman at conncoll.edu
Wed Jan 19 14:20:18 CET 2005
Ah! Of course! Thank you. But no, you're right, it doesn't work on the
find-the-longest problem. (Result is a recursion overrun; hm.)
"We are working on some minor configuration/security issues which may
result in odd behavior over the next 24-48 hours." -- notice on a BBS
On Jan 18, 2005, at 5:08 PM, Nicholas Riley wrote:
> On Tue, Jan 18, 2005 at 04:40:54PM -0500, Charles Hartman wrote:
>> find 'abca' in string 'abcabca'
>>
>> Every *single* RE I can think of misses the second instance (beginning
>> in position 3) in the first example, because it's eaten the beginning
>> of it in finding the first instance.
>
> Lookahead will do this one, e.g.:
>
> In [39]: [m.span() for m in re.finditer(r'(?=abca)', 'abcabca')]
> Out[39]: [(0, 0), (3, 3)]
>
>> find longest '(a[ab])+' in string 'baabaaabba'
>
> I don't think even Perl regexps can do the second one, but I might be
> wrong. Neither greedy or non-greedy quantifiers will compare match
> length. You could get a list of all matches and pick the longest one
> yourself.
>
> --
> Nicholas Riley <njriley at uiuc.edu> |
> <http://www.uiuc.edu/ph/www/njriley>
>
More information about the Pythonmac-SIG
mailing list