Skip to content Skip to sidebar Skip to footer

Python Regular Expression Date Formate

Trying to write a RE to recognize date format mm/dd in Python reg = '((1[0-2])|(0?[1-9]))/((1[0-9])|(2[0-9])|(3[0-1])|(0?[0-9]))' match = re.findall(reg, text, re.IGNORECASE) print

Solution 1:

dont't use re.findall. use re.match:

reg = "((0?[1-9])|(1[0-2]))/((1[0-9])|(2[0-9])|(3[0-1])|(0?[0-9]))"match = re.match(reg, text, re.IGNORECASE)
printmatch.group()

Solution 2:

The other answers are more direct, but you could also add an extra pair of braces around your regex:

reg = "(((0?[1-9])|(1[0-2]))/((1[0-9])|(2[0-9])|(3[0-1])|(0?[0-9])))"

Now findall will give you:

[('4/13', '4', '4', '', '13', '13', '', '', '')]

You can now extract '4/13' from above.

Post a Comment for "Python Regular Expression Date Formate"