0

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 match

For text = '4/13' it gives me

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

but not

'4/13'

Thanks, Cheng

cheng
  • 6,596
  • 6
  • 25
  • 27
  • Possible duplicate of [Converting string into datetime](http://stackoverflow.com/questions/466345/converting-string-into-datetime) –  May 07 '12 at 15:04

2 Answers2

3

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)
print match.group()
mata
  • 67,110
  • 10
  • 163
  • 162
1

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.

mohit6up
  • 4,088
  • 3
  • 17
  • 12
  • by the way, how do I get rid of the 4s and 13s? – cheng May 07 '12 at 15:10
  • You can't, using `findall` in this case. `findall` finds all patterns within pairs of braces. So you end up with the outer pattern `'4/13'` you are looking for along with some inner patterns like `'4'`, `'13'`... – mohit6up May 07 '12 at 15:21