【发布时间】:2019-06-13 00:15:45
【问题描述】:
我的正则表达式返回一个项目列表,我只需要从中获取日期范围。该列表并不总是具有特定索引处的日期范围。
我尝试先将列表转换为字符串,然后仅提取日期范围:
possible_billing_periods = list(re.findall(r'Billing Period: (.*)|Billing period: (.*)|Billing Period (.*)|Billing period (.*)|period (.*)|period: (.*)', data))
billing_period = str(possible_billing_periods)
for k in billing_period.split("\n"):
if k != ['(A-Za-Z0-9)']:
billing_period_2 = re.sub(r"[^a-zA-Z0-9]+", ' ', k)
print(possible_billing_periods)
输出:[('', '', '', '', 'Tel', ''), ('21-june-2018 - 25 -September-2018', '', '', '', '', '')]
预期结果:21-june-2018 25-September-2018
结果得到:Tel 21 june 2018 25 September 2018
样本数据:
2018 年 8 月 28 日开始指数:B1 0
2018 年 8 月 28 日开始指数:E1 0
计费期:2018 年 6 月 21 日 - 2018 年 9 月 25 日
预计下一读:2018 年 12 月 25 日
【问题讨论】:
-
你能不能也给我们看看样本
data? -
我们需要看几行
data -
2018 年 8 月 28 日开始指数:B1 0 2018 年 8 月 28 日开始指数:E1 0 计费期:2018 年 6 月 21 日 - 2018 年 9 月 25 日预计下一读:2018 年 12 月 25 日
-
您需要的日期是否总是在以
'Billing Period'开头的行中?你需要正则表达式来处理大写/小写吗? -
打印语句输出中的 ('', '', '', '', 'Tel', '') 表明您的数据与正则表达式中的第五组匹配。那是“|句号 (.*)|”,其中 .* 匹配“Tel”。