regular expression - matches

Simon Forman rogue_pedro at yahoo.com
Fri Jul 21 12:18:30 EDT 2006


John Salerno wrote:
> Simon Forman wrote:
>
> > Python's re.match() matches from the start of the string, so if you
> > want to ensure that the whole string matches completely you'll probably
> > want to end your re pattern with the "$" character (depending on what
> > the rest of your pattern matches.)
>
> Is that necessary? I was thinking that match() was used to match the
> full RE and string, and if they weren't the same, they wouldn't match
> (meaning a begin/end of string character wasn't necessary). That's wrong?

My understanding, from the docs and from dim memories of using
re.match() long ago, is that it will match on less than the full input
string if the re pattern allows it (for instance, if the pattern
*doesn't* end in '.*' or something similar.)

I'd test this, though, before trusting it.

What the heck, I'll do that now:

>>> import re
>>> re.match('ab', 'abcde')
<_sre.SRE_Match object at 0xb6ff8790>
>>> m = _
>>> m.group()
'ab'
>>> print re.match('ab$', 'abcde')
None


Yup!  That's the case.

Peace,
~Simon




More information about the Python-list mailing list