试图用Python编写一个RE来识别日期格式mm/dd
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对于text = '4/13',它给了我
[('4', '4', '', '13', '13', '', '', '')]但不是
'4/13'谢谢你,程
发布于 2012-05-07 15:08:03
其他答案更直接,但您也可以在正则表达式周围添加另一对大括号:
reg = "(((0?[1-9])|(1[0-2]))/((1[0-9])|(2[0-9])|(3[0-1])|(0?[0-9])))"
现在,findall将给您:
[('4/13', '4', '4', '', '13', '13', '', '', '')]
现在您可以从上面提取'4/13'。
发布于 2012-05-07 15:04:35
不要使用re.findall。使用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()https://stackoverflow.com/questions/10484300
复制相似问题