Regular Expression To Return All Match Occurrences
I have text like below- 02052020 02:40:02.445: Vacation Allowance: 21; nnnnnn Vacation Allowance: 22;nnn I want to extract the below in Python- Vacation Allowance: 21 Vacation Allo
Solution 1:
The issue is with the regular expression used.
The (.*)
blocks are accepting more of the string than you realize - .*
is referred to as a greedy operation and it will consume as much of the string as it can while still matching. This is why you only see one output.
Suggest matching something like Vacation Allowance:\s*\d+;
or similar.
text = '02/05/2020 Vacation Allowance: 21; 02/05/2020 Vacation Allowance: 22; nnn'
m = re.findall('Vacation Allowance:\s*(\d*);', text, re.M)
print(m)
result: ['21', '22']
Solution 2:
In Javascript it would be 'text'.match(/\bVacation Allowance: \d+/g)
You need the global attribute g
Post a Comment for "Regular Expression To Return All Match Occurrences"