data regex match

Heiko Wundram me+python at modelnine.org
Tue May 2 17:27:57 EDT 2006


Am Dienstag 02 Mai 2006 23:06 schrieb Gary Wessle:
> Hi
>
> I am having an issue with this match
>
> tx = "now 04/30/2006 then"
> data = re.compile('(\d{2})/\1/\1\1', re.IGNORECASE)

As always, use a raw string for regular expressions. \d is being interpreted 
to mean an ascii character, and not to mean the character class you're trying 
to reference here.

Second: \1 (if properly quoted in the string) matches the first group exactly. 
Your regex would only match 20/20/2020 or strings of such format.

Third: IGNORECASE is irrelevant here, you're not trying to match letters, are 
you?

Anyway, the following works:

dateregex = re.compile(r"(\d{2})/(\d{2})/(\d{4})")
m = dateregex.search(tx)
if m:
    print m.groups()
else:
    print "No match."

--- Heiko.



More information about the Python-list mailing list