You can use re.match
to find only the characters:
>>> import re
>>> s=r"""99-my-name-is-John-Smith-6376827-%^-1-2-767980716"""
>>> re.match('.*?([0-9]+)$', s).group(1)
'767980716'
Alternatively, re.finditer
works just as well:
>>> next(re.finditer(r'\d+$', s)).group(0)
'767980716'
Explanation of all regexp components:
.*?
is a non-greedy match and consumes only as much as possible (a greedy match would consume everything except for the last digit).[0-9]
and\d
are two different ways of capturing digits. Note that the latter also matches digits in other writing schemes, like ୪ or ൨.- Parentheses (
()
) make the content of the expression a group, which can be retrieved withgroup(1)
(or 2 for the second group, 0 for the whole match). +
means multiple entries (at least one number at the end).$
matches only the end of the input.