且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python - 检查字符串中的最后一个字符是否为数字

更新时间:2022-11-16 18:06:13

import re
m = re.search(r'\d+$', string)
# if the string ends in digits m will be a Match object, or None otherwise.
if m is not None:
    print m.group()

\d 匹配一个数字,\d+ 表示匹配一个或多个数字(贪婪:匹配尽可能多的连续数字).而 $ 表示匹配字符串的结尾.


\d matches a numerical digit, \d+ means match one-or-more digits (greedy: match as many consecutive as possible). And $ means match the end of the string.