This is a great regular expression for dates… However it hangs indefinitely on this one page I tried… I wanted to try this page ( http://pleac.sourceforge.net/pleac_python/datesandtimes.html ) for the fact that it does have lots of dates on it and I want to grab all of them. I don’t understand why it is hanging when it doesn’t on other pages… Why is my regexp hanging and/or how could I clean it up to make it better/efficient ?
Python Code:
monthnames = "(?:Jan\w*|Feb\w*|Mar\w*|Apr\w*|May|Jun\w?|Jul\w?|Aug\w*|Sep\w*|Oct\w*|Nov(?:ember)?|Dec\w*)"
pattern1 = re.compile(r"(\d{1,4}[\/\\\-]+\d{1,2}[\/\\\-]+\d{2,4})")
pattern4 = re.compile(r"(?:[\d]*[\,\.\ \-]+)*%s(?:[\,\.\ \-]+[\d]+[stndrh]*)+[:\d]*[\ ]?(PM)?(AM)?([\ \-\+\d]{4,7}|[UTCESTGMT\ ]{2,4})*"%monthnames, re.I)
patterns = [pattern4, pattern1]
for pattern in patterns:
print re.findall(pattern, s)
btw… when i say im trying it against this site.. I’m trying it against the webpage source.
You should read Mastering Regular Expressions. The problem is:
which takes exponential time. Try using:
which should match the same things but take linear time. Having checked your example, this does indeed speed things up.
You also appear to have accidentally introduced capturing groups into your pattern at some point: change e.g. (AM) to (?:AM) to fix that. This gets us the following output from your example above:
To get into details (which the book I reference is very good at), * and + work (in NFAs like python’s re) rather like a loop. The original pattern’s inner loop will match against a long string of digits, but when the subsequent pattern fails to match, it will “give them up” one at a time. The outer loop will then rerun the inner loop on the remaining pattern, and of course it will immediately grab the digits again. Each time one instance of the inner loop relinquishes a digit, a new copy will be summoned to grab it again. Eventually, once the engine has gone through every possible way of splitting that string of digits (an exponential number of possibilities), it will move the starting point forwards one character…and try again.
On another note, your pattern looks a bit bonkers 😉