matching any number of any character

Carel Fellinger cfelling at iae.nl
Tue Aug 24 00:10:35 CEST 1999


Lyn A Headley <laheadle at boguscs.uchicago.edu> wrote:

> accomplish such a task.  I thought this would work:

it does...

>>>> import regex
>>>> rx = regex.compile('\(.\|\n\)+')
>>>> rx.match('abc')
> 3

you see, it just matched 3 characters, but...

>>>  rx.group(1)
> 'c'

here you are tricked by the semantics of a quantified grouping operator.
A quantified group only remembers the last match, and as your group only
matches a single character, 'c' it is:)
(( shouldn't this be documented in the re module? it has stung more people
   recently ))

> but I wanted 'abc'!

so you have to refer to the whole match (rx.group(0)) or get the
quantification inside a group. e.g.

>>> rx = regex.compile('\(\(.\|\n|)+\)')
>>> rx.group(1)
'abc'
>>> rx.group(2)
'c'

-- 
groetjes, carel




More information about the Python-list mailing list