python regex: get end digits from a string

前端 未结 7 682
予麋鹿
予麋鹿 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 21:02

    Nice and simple with findall:

    import re
    
    s=r"""99-my-name-is-John-Smith-6376827-%^-1-2-767980716"""
    
    print re.findall('^.*-([0-9]+)$',s)
    
    >>> ['767980716']
    

    Regex Explanation:

    ^         # Match the start of the string
    .*        # Followed by anthing
    -         # Upto the last hyphen
    ([0-9]+)  # Capture the digits after the hyphen
    $         # Upto the end of the string
    

    Or more simply just match the digits followed at the end of the string '([0-9]+)$'

提交回复
热议问题