python regex: get end digits from a string

前端 未结 7 672
予麋鹿
予麋鹿 2020-12-08 20:29

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\"\         


        
7条回答
  •  醉梦人生
    2020-12-08 20:49

    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)
    

提交回复
热议问题