python regex: get end digits from a string

前端 未结 7 670
予麋鹿
予麋鹿 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:53

    Your Regex should be (\d+)$.

    • \d+ is used to match digit (one or more)
    • $ is used to match at the end of string.

    So, your code should be: -

    >>> s = "99-my-name-is-John-Smith-6376827-%^-1-2-767980716"
    >>> import re
    >>> re.compile(r'(\d+)$').search(s).group(1)
    '767980716'
    

    And you don't need to use str function here, as s is already a string.

提交回复
热议问题