尝试编写一个 RE 来识别 Python 中的日期格式 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'
谢谢,程
不要使用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()
其他答案更直接,但您也可以在正则表达式周围添加一对额外的大括号:
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'从上面提取。