I want to match the last N digits in a line of text. I know I can use re.findall to simply extract all digits and then count back N but I am interested to know if I can extract N groups using re.match. I have this:
line = 'humpty dumpty 25 1, 2, 3, 4, 5, 6'
N = 6
p = re.compile('^(.+)(\D+\d+){{{0}}}$'.format(N))
m = re.match(p, line)
I get a match OK. However I want to access each of 1, 2, 3, 4, 5, 6 but all I get is:
>>> m = re.match(p, line)
>>> m.group(0)
'humpty dumpty 25 1, 2, 3, 4, 5, 6'
>>> m.group(1)
'humpty dumpty 25'
>>> m.group(2)
', 6'
>>> m.group(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: no such group
I want to see a group for each digit. Can re.match be used in the way I am attempting?
Thanks.