There is a need to extract the digits before h
, m
, and s
into their own groups. The objective is to capture all three groups if possible, but if one or two groups are missing, then the last group(s) should be captured.
Currently, the regex
/(\d+)(?=h)|(\d+)(?=m)|(\d+)(?=s)/g
is generating 12 matches with a single group in each match, as demonstrated in the image below and in this regex101.
https://i.sstatic.net/ZYUMY.png
However, the goal is to have 7 matches with different group configurations. The first match should contain 3 separate groups, the following three matches should have 2 groups each, and the last three matches should contain 1 group each.
Therefore, the desired output is as follows:
- Match 1:
11h22m33s
- Group 1:
11
- Group 2:
22
- Group 3:
33
- Group 1:
- Match 2:
11h22m
- Group 1:
11
- Group 2:
22
- Group 1:
- Match 3:
11h33s
- Group 1:
11
- Group 2:
33
- Group 1:
- Match 4:
22m33s
- Group 1:
22
- Group 2:
33
- Group 1:
- Match 5:
11h
- Group 1:
11
- Group 1:
- Match 6:
22m
- Group 1:
22
- Group 1:
- Match 7:
33s
- Group 1:
33
- Group 1:
Edit
The test strings could be embedded within other text, like 08:00 + 11h
. Refer to https://regex101.com/r/RWA9Oy/1