用于提取月份和年份的Python正则表达式

时间:2019-03-20 01:20:33

标签: python regex parsing

我想从字符串中提取月份和年份。例如:

如果我有字符串From August 2017 - September 2018,则应该将'August 2017''September 2018'分为两组。我尝试了以下方法:

import re

regex = r'(\b\d{1,2}\D{0,3}\b-)?\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)\D?(\d{1,2}\D?)?\D?((19[7-9]\d|20\d{2})|\d{2})'
experience = re.findall(regex, 'August 2017 - Sep 2018')
print(experience)

这将返回[('', '', '20', '17', ''), ('', '', '20', '18', '')]

我还尝试了 re.search

import re

regex = r'(\b\d{1,2}\D{0,3}\b-)?\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)\D?(\d{1,2}\D?)?\D?((19[7-9]\d|20\d{2})|\d{2})'
experience = re.search(regex, 'August 2017 - Sep 2018')
print(experience.group())

这仅返回August 2017

我们可以使用一些正则表达式来提取两个日期吗?

1 个答案:

答案 0 :(得分:0)

您的意思是这样吗? Regex demo

import re

string = "From August 2017 - September 2018"
month = re.search("(?P<fmonth>\w+.\d+)\s+\-\s+(?P<smonth>\w+.\d+)", string)
month = month.groups()

print (month)

输出:

('August 2017', 'September 2018')