matching any number of any character

Tim Peters tim_one at email.msn.com
Mon Aug 23 02:06:59 EDT 1999


[Lyn A Headley]
> To do it, I want to use the regex module, heretical though that may
> be.

Not heretical so much as ornery and self-destructive <wink>.

> I think my brain is missing a piece, because I can't seem to
> accomplish such a task.  I thought this would work:
>
> >>> import regex
> >>> rx = regex.compile('\(.\|\n\)+')
> >>> rx.match('abc')
> 3
> >>  rx.group(1)
> 'c'
>
> but I wanted 'abc'!

Persist:

>>> rx.group(0)
'abc'
>>>

The thing inside your parens only matches one character; repeating it doesn't
change that; you get back the character (c) it matched last.  Life is easier
with re:

>>> import re
>>> rx = re.compile("(.+)", re.DOTALL)
>>> m = rx.match("abc")
>>> m.group(1)
'abc'
>>>

this-is-your-brain-that-was-your-brain-on-regex-ly y'rs  - tim






More information about the Python-list mailing list