Exact match with regular expression
Rob Williscroft
rtw at freenet.co.uk
Sun Oct 26 13:21:14 EDT 2008
Mr.SpOOn wrote in news:mailman.3069.1225039892.3487.python-
list at python.org in comp.lang.python:
> Hi,
> I'd like to use regular expressions to parse a string and accept only
> valid strings. What I mean is the possibility to check if the whole
> string matches the regex.
>
> So if I have:
>
>>>> p = re.compile('a*b*')
>
> I can match this: 'aaaaaabbb'
>
>>>> m = p.match('aaaaaabbb')
>>>> m.group()
> 'aaaaaabbb'
>
> But I'd like to get None with this: 'aabDDDDb'
> Instead it matches the first part:
>
>>>> m = p.match('aabDDDDb')
>>>> m.group()
> 'aab'
Read (and bookmark) this:
http://www.python.org/doc/2.5.2/lib/re-syntax.html
You want the 3rd item down about the "$" special character.
>>> p = re.compile('a*b*$')
>>> m = p.match('aabDDDDb')
>>> m is None
True
>>> m = p.match('aaaaaabbb')
>>> m.group()
'aaaaaabbb'
Rob.
--
http://www.victim-prime.dsl.pipex.com/
More information about the Python-list
mailing list