How can I find the last number in any big string?
For eg in the following string I want 47 as the output:
'tr bgcolor="aa77bb"td>font face="verdana"color="white" size="2">b>Total/b>/font>/td>\td>font face="verdana"color="white" size="2">b>47/b>/font>/td>/tr>'
PS: We don't know the number. The number 47 is just an example. It can be any number from 0 to 900.
解决方案>>> import re
>>> text = 'tr bgcolor="aa77bb"td>font face="verdana"color="white" size="2">b>Total/b>/font>/td>\td>font face="verdana"color="white" size="2">b>47/b>/font>/td>/tr>'
>>> re.findall(r'\d+', text)[-1]
'47'
If you need to match floating points there's always this
For very long strings this is more efficient:
re.search(r'\d+', text[::-1]).group()[::-1]