data regex match

Fredrik Lundh fredrik at pythonware.com
Tue May 2 17:23:46 EDT 2006


Gary Wessle wrote:

> I am having an issue with this match
>
> 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

really?  your pattern matches two digits, followed by a slash, followed
by a byte with the ASCII value 1, followed by a slash, followed by two
bytes with the ASCII value 1.

    >>> '(\d{2})/\1/\1\1'
    '(\\d{2})/\x01/\x01\x01'

in case you meant to write

    r'(\d{2})/\1/\1\1'

(which is the same thing as '(\\d{2})/\\1/\\1\\1')

it's still not close; that pattern matches two digits, followed by a slash,
followed by the *same* two digits, followed by a slash, followed by the
same two digits, followed by the same two digits.

in other words, dates like 20/20/2020 and 12/12/1212.

try

    '\d\d/\d\d/\d\d\d\d'

instead.

> what went wrong?

(insert obligatory jwz quote here)

</F>






More information about the Python-list mailing list