【发布时间】:2020-01-13 04:56:48
【问题描述】:
我有一个正则表达式,可以检测给定段落文本中的出生日期。
import re
dob = re.compile(r'(?:\bbirth\b|\bbirth(?:day|date)).{0,20}\n? \b((?:(?<!\:)(?<!\:\d)[0-3]?\d(?:st|nd|rd|th)?\s+(?:of\s+)?(?:jan\.?|january|feb\.?|february|mar\.?|march|apr\.?|april|may|jun\.?|june|jul\.?|july|aug\.?|august|sep\.?|september|oct\.?|october|nov\.?|november|dec\.?|december)|(?:jan\.?|january|feb\.?|february|mar\.?|march|apr\.?|april|may|jun\.?|june|jul\.?|july|aug\.?|august|sep\.?|september|oct\.?|october|nov\.?|november|dec\.?|december)\s+(?<!\:)(?<!\:\d)[0-3]?\d(?:st|nd|rd|th)?)(?:\,)?\s*(?:\d{4})?|\b[0-3]?\d[-\./][0-3]?\d[-\./]\d{2,4})\b',re.IGNORECASE | re.MULTILINE)
data = " Hi This is Goku and my birthday is on 6th Aug but to be clear it is on 1994-08-06."
l = dob.findall(data)
print(l)
o/p: ['6th Aug ']
我只想再添加一个特征,比如如果文本中出现这种格式 YYYY-MM-DD 的内容,那么这也应该是出生日期。
(其中 YYYY --> 19XX-20XX ,MM --> 01-12 ,DD --> 01-31)
例如:
data = " Hi This is Goku and my birthday is on 6th Aug but to be clear it is on 1994-08-06."
那么输出应该是
输出:['6th Aug ', '1994-08-06']
我在哪里可以在正则表达式中添加该部分,以便它也能检测到这种 YYYY-MM-DD 格式。??
【问题讨论】:
-
所以基本上是一个检测字符串中任何日期的正则表达式?
-
不完全是,我需要上面提到的输出。
-
这个字符串怎么样:
Hi my name is August von Spiff the third, but people call me Aug 3rd. I'm gonna celebrate my birthday on May 4th (2019-05-04), but the actual day is on the 7th of may? -
2019-05-04 这应该被检测到。并且输出应该是 ['May 4th', '2019-05-04']
-
@JinKazama 但是 August von Spiff 的生日是 5 月 7 日,那你为什么要提取错误的数据?