python regex: get end digits from a string

python, regex

Solution

I have been playing around with several of these solutions, but many seem to fail if there are no numeric digits at the end of the string. The following code should work.

import re

W = input("Enter a string:")
if re.match('.*?([0-9]+)$', W)== None:
    last_digits = "None"
else:
    last_digits = re.match('.*?([0-9]+)$', W).group(1)
print("Last digits of "+W+" are "+last_digits)

Problem

I am quite new to python and regex (regex newbie here), and I have the following simple string: ``` s=r"""99-my-name-is-John-Smith-6376827-%^-1-2-767980716""" ``` I would like to extract only the last digits in the above string i.e 767980716 and I was wondering how I could achieve this using python regex. I wanted to do something similar along the lines of: ``` re.compile(r"""-(.*?)""").search(str(s)).group(1) ``` indicating that I want to find the stuff in between (.*?) which starts with a "-" and ends at the end of string - but this returns nothing.. I was wondering if anyone could point me in the right direction.. Thanks.

Original source