Regex match string that ends with number

后端 未结 5 1561
孤独总比滥情好
孤独总比滥情好 2020-12-21 01:13

What is a regex to match a string that ends with a number for example

\"c1234\" - match
\"c12\" - match
\"c\" - no match

Tried this but it

5条回答
  •  情深已故
    2020-12-21 01:58

    dynamic way would be:

    import re
    word_list = ["c1234", "c12" ,"c"]
    for word in word_list:
        m = re.search(r'.*\d+',word)
        if m is not None:
            print(m.group(),"-match")
        else:
            print(word[-1], "- nomatch")
    

    RESULT:

    c1234 -match
    c12 -match
    c - nomatch
    

提交回复
热议问题