re.match and non-alphanumeric characters

John Machin sjmachin at lexicon.net
Mon Nov 17 01:02:35 CET 2008


On Nov 17, 10:19 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> John Machin schrieb:
>
> > On Nov 17, 4:44 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>
> >>  Match matches the whole string.
>
> > *ONLY* if the pattern ends with "$" or r"\Z"
>
> You think so?
>
> import re
>
> rex = re.compile("abc.*def")
>
> if rex.match("abc0123455678def"):
>      print "matched"
>

OK, I'll try again:

The following 3-tuples represent (pattern, string,
matched_portion_of_string):
('abc', 'abc', 'abc')
('abc', 'abcdef', 'abc')
('abc$', 'abc', 'abc')
('abc$', 'abcdef', '<no match>')

Saying "Match matches the whole string" is incorrect; see the second
case. If you want to ensure that the whole string matches the pattern,
the pattern needs to be terminated by "$" or "\Z".



More information about the Python-list mailing list