minutes_pattern = re.compile(
r"""
(?:(?P<hours>\d+)H)?
(?:(?P<minutes>\d+)M)?
(?:(?P<seconds>\d+)S)?
""",
re.VERBOSE,
)
I'm trying to capture all HOURS, MINUTES, SECONDS. However, these may or may not exist: Examples:
PT30H20M10S
PT20M10S
PT20M
PT3010S
PT30H
I'm thinking (?:(?P<hours>\d+)H)? says:
- ?: non capturing group
- ?P hours group name
- \d+ any number of digits
- H must exist
- )? this group may or may not exist
But this compile does not capture any of the 3 groups. Help please
regex = minutes_pattern.search(duration)
print(regex.groups())
This only returns None
(None, None, None)
Ideally this would show (for the 1st example):
group hours: 30
group minutes: 20
group seconds: 10
And for the 2nd example:
group hours: None
group minutes: 20
group seconds: 10