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

python regex problem extract numbers from days hours minutes

我正在学习 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

我正在努力修复后视问题。如何解决问题?

为什么添加 ?<=? 看,我将组添加到您的正则表达式并添加缺少的 space 分隔符

然后您可以匹配正则表达式和 select 组。

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 可以帮助您理解每个元素的作用。