input_string = '7:30 p.m. 7:20 p.m.every Mon., any Wed.Thu,fri'
我想这样:
print match.group('day')= mon, wed, thu, fri
print match.group('starttime')= 7:30 p.m, 7:20 p.m
我试过了:
match = re.search(r'(?P<starttime>([\d]*:[\d]*)(?:[ .apm]*m)?)
.*?(?P<day>(?i)(?:mon(?:day)?|tue(?:sday)?|wed(?:nesday)?|
thu(?:rsday)?|fri(?:day)?|sat(?:urday)?|sun(?:day)?))'
, input_string)
但我没有做对。
答案 0 :(得分:0)
您需要为时间和日期部分使用单独的模式,然后使用.findall()
查找多个匹配项:
>>> re.findall(r'(?P<starttime>[\d]*:[\d]*(?:[ .apm]*m)?)', input_string)
['7:30 p.m', '7:20 p.m']
>>> re.findall(r'(?P<day>(?i)(?:mon(?:day)?|tue(?:sday)?|wed(?:nesday)?|thu(?:rsday)?|fri(?:day)?|sat(?:urday)?|sun(?:day)?))', input_string)
['Mon', 'Wed', 'Thu', 'fri']
然后命名组真的是多余的,以下是:
re.findall(r'[\d]*:[\d]*(?:[ .apm]*m)?', input_string)
re.findall(r'(?i)mon(?:day)?|tue(?:sday)?|wed(?:nesday)?|thu(?:rsday)?|fri(?:day)?|sat(?:urday)?|sun(?:day)?', input_string)