Question

I'm trying to parse dates with regex, using groups, but python is returning empty lists. I'm not doing anything fancy, just 12/25/10 sort of stuff. I want it to reject 12/25-10 though.

date = re.compile("\d{1,2}([/.-])\d{1,2}\1\d{2}")

I've tried online regex libraries, but their solutions don't seem to run either. Any ideas?

Sample input: "Hello today is 10/18/10, and the time is 10:50am" Hopeful output: "10/18/10"

I'm running Python 2.5.

Was it helpful?

Solution

Use a raw string:

date = re.compile(r"\d{1,2}([/.-])\d{1,2}\1\d{2}")

Otherwise, the \1 in the string literal is interpreted as the character 1 (Start of Heading).

EDIT: To add groups for the date components, use:

re.compile(r"(\d{1,2})([/.-])(\d{1,2})\2(\d{2})")

OTHER TIPS

You should use Python's builtin strptime.

No doubt overkill, but the "parsedatetime" library has been working for me: http://code.google.com/p/parsedatetime/

It does use regexes internally, but does a lot more than parse MM/DD/YY formats.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top