I am using RegEx in Python and I'm having some trouble to find all the substrings matching a given pattern. For example:
import re
pattern = '\d{8}[TRWAGMYFPDXBNJZSQVHLCKE]$'
text = 'I want to match this two: 01234567A and this 89012345B'
When I apply the RegEx, I would expect to obtain both 89012345B
and 01234567A
, but I am only getting the last value (89012345B
). What I've tried so far is:
match = re.search(pattern, text)
print(match.group())
>>> '89012345B'
match = re.finditer(pattern, text)
print([match.group() for match in matches])
>>> ['89012345B']
match = re.findall(pattern, text)
print(match)
>>> ['89012345B']
As you can see, this only returns the last match. What am I missing? How could I get both?