I want to fetch the digit of the last occurance of the substring
input : "abc1 foo barabc2 abc3 abc4 foobar"
output : 4
I want to fetch the digit of the last occurance of the substring
input : "abc1 foo barabc2 abc3 abc4 foobar"
output : 4
You can use re.findall
:
import re
s = "abc1 foo barabc2 abc3 abc4 foobar"
print(re.findall('\d+', s)[-1])
Output:
4
Well if that's the only thing you want to get then I wouldn't use regexp
at all, instead:
s = "abc1 foo barabc2 abc3 abc4 foobar"
print([c for c in s if c.isdigit()][-1])
I hope you were looking for something like that.