python正则表达式格式化 [英] python regular expression date formate

查看:150
本文介绍了python正则表达式格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试写一个RE来识别Python中的日期格式mm / dd

  reg =((1 [ 2])|(0→[1-9]))/((1 [0-9])|(2 [0-9])|(3 [0-1])|(0?[0-9 ]))
match = re.findall(reg,text,re.IGNORECASE)
打印匹配






对于 text ='4/13'它给我

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

但不是

 '4/13'

谢谢,

解决方案

其他答案更直接,但您也可以在正则表达式周围添加一对额外的括号:


$ b $ (1 [0-9])|(2 [0-9])|(1 [0-2]))/((1 [0-9])|(2 [ 9])|(3 [0-1])|(0?[0-9])))



现在 findall 将给你:



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



您现在可以从上方提取'4/13'


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

解决方案

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.

这篇关于python正则表达式格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆