matching any number of any character

Carel Fellinger cfelling at
Mon Aug 23 18:10:35 EDT 1999

Lyn A Headley <laheadle at> 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...

> '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 ( or get the
quantification inside a group. e.g.

>>> rx = regex.compile('\(\(.\|\n|)+\)')

groetjes, carel

More information about the Python-list mailing list