python正则表达式问题从天、小时、分钟中提取数字



我正在学习python正则表达式,并想知道如何从中提取数字x days y hours z minutes

注意:没有月或秒,只允许一天、分钟和秒中的一个或多个。

我的尝试

import re
s1 = '5 days 19 hours 30 minutes'
s2 = '5 days'
s3 = '19 hours'
s4 = '5 days 19 hours'
pat = r'((d+)(?<=sdays))?((d+)(?<=shours))?((d+)(?<=sminutes))?'

d,h,m = re.findall(pat,s)
Note: 2 days 3 hours ==> d=2 h=3
2 hours 3 minutes ==> h=2 m=3

我正在努力解决外观问题。如何解决问题?

为什么添加?<=?听着,我在正则表达式中添加组,并添加缺少的空格分隔

然后,您可以匹配您的正则表达式并选择组。

Python 3.7

import re
s4 = '5 days 19 hours'
pat = r'(?P<days>(d+)(sdays))? ?(?P<hours>(d+)(shours))? ?(?P<minutes>(d+)(sminutes))?'
match = re.match(pat, s4)
if match:
print(match.groupdict())  # print all groups
# Output: {'days': '5 days', 'hours': '19 hours', 'minutes': None}

如果您只想匹配值的编号,而不是名称和编号,则需要使用下一种模式:

r'((?P<days>d+) days)? ?((?P<hours>d+) hours)? ?((?P<minutes>d+) minutes)?'
"""
Here I deconstruct the pattern,
then you can look at it and the next time you can make your own without help.
((?P<days>d+) days)?          Match numbers + space + "days"
?                             Match space
((?P<hours>d+) hours)?        Match numbers + space + "hours"
?                             Match space
((?P<minutes>d+) minutes)?    Match numbers + space + "minutes"
If you want the group "days" return you the number and the word "days" yo need to use it as:
(?P<days>d+ days)
"""

https://regex101.com/是一个尝试你的模式的好地方。它有一个很好的IDE,可以帮助你了解每个元素的作用

最新更新