2

I have a regex that matches numbers and I want to get the position of the last-matching number.

This is what I got right now:

def find_last_match_pos(pattern, s):
    match = None
    for match in re.finditer(pattern, s):
        pass
    return match.start() if match else -1

Can anyone think of a more pythonic way to do it?

seriousdev
  • 7,519
  • 8
  • 45
  • 52

1 Answers1

3

Why not just use findall?

s.rfind(re.findall(pattern, s)[-1])
mr2ert
  • 5,146
  • 1
  • 21
  • 32