0

I have to find the last occurrence of an ip address in a string using regex. I found this

addr = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', program[line])

which finds the first occurrence. How can I change it to find the last one?

Christian Dean
  • 20,986
  • 7
  • 47
  • 78
  • This is not a duplicate of either of those questions. To the OP: if this wasn't marked duplicate I could give you a detailed explanation. The definitive answer is `.*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})` where the ip is in capture group 1. So, `if ( (match=re.search(r'.*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', program[line]) ) ip = match.group(1)` –  Jul 07 '17 at 18:02

1 Answers1

1
re.findall(pattern, string)[-1]
Alex Hall
  • 33,530
  • 5
  • 49
  • 82