日期正则表达式python [英] Date regex python

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

问题描述

我正在尝试匹配日期格式为(month dd,yyyy)的字符串中的日期。当我在下面使用正则表达式模式时,看到的内容让我感到困惑。它仅匹配以日期开头的字符串。我想念什么?

I am trying to match dates in a string where the date is formatted as (month dd, yyyy). I am confused by what I see when I use my regex pattern below. It only matches strings that begin with a date. What am I missing?

 >>> p = re.compile('[A-z]{3}\s{1,}\d{1,2}[,]\s{1,}\d{4}')
 >>> s = "xyz Dec 31, 2013 - Jan 4, 2014"
 >>> print p.match(s).start()
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 AttributeError: 'NoneType' object has no attribute 'start'

 >>> s = "Dec 31, 2013 - Jan 4, 2014"
 >>> print p.match(s).start()
 0 #Correct


推荐答案

使用搜索方法代替匹配。 Match比较整个字符串,但搜索找到匹配的部分。

Use the search method instead of match. Match compares the whole string but search finds the matching part.

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

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