data regex match

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Tue May 2 17:28:56 EDT 2006


Gary Wessle:
>tx = "now 04/30/2006 then"
>data = re.compile('(\d{2})/\1/\1\1', re.IGNORECASE)
>d = data.search(tx)
>print d
>
>Nono
>I was expecting 04/30/2006

You should expect: NameError: name 're' is not defined

> what went wrong?

\1 matches the content of the first group, which is '04'. It doesn't match
'30', '20' and '06'.

Also, you'll need to use a raw string, or backslash your backslashes.

>>> import re
>>> tx = "now 04/04/0404 then"
>>> data = re.compile(r'(\d{2})/\1/\1\1', re.IGNORECASE)
>>> d = data.search(tx)
>>> print d
<_sre.SRE_Match object at 0x01287F60>

-- 
René Pijlman



More information about the Python-list mailing list