如何编写Python正则表达式来验证数字?
以下代码验证一个数字,该数字等于“2018”
示例
import re s = '2018' match = re.match(r'\b2018\b',s) print match.group()
输出结果
这给出了输出
2018
示例
以下代码验证任何五位数的正整数
import re
s = '2346'
match = re.match(r'(?<!-)\b[1-9]\d{4}\b',s)
print match
s2 = '56789'
match = re.match(r'(?<!-)\b[1-9]\d{4}\b',s2)
print match.group()输出结果
None 56789
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短