1

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.

1
  • You cannot use it that way in Python. You need a separate method that will do that. Commented Nov 2, 2015 at 23:10

1 Answer 1

5

your problem is that if you do a repeat of groups, it will only capture the last instance of that group. you have to set a different group for each capture you want. this line below works:

p = re.compile('^(.+)' + '(\D+\d+)'*N + '$')

EXAMPLE:

>>> m = re.match(p, line)
>>> m.groups()
('humpty dumpty 25', ' 1', ', 2', ', 3', ', 4', ', 5', ', 6')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.