python re用于验证字符串

pre*_*lpi 4 python regex

以下是代码:

import re
string = "02:222222"
if re.match(r'[a-fA-F0-9]+[a-fA-F0-9]+:+[a-fA-F0-9]+[a-fA-F0-9]+$',string):
    print "pass"
else:
    print "fail"
Run Code Online (Sandbox Code Playgroud)

上面的代码是打印"通过"

我的预期输出应该是"失败"

以下是几个例子:

string = 00:20
expected output: pass
string = 00:202
expected ouput: fail
string = 00:2z
expected output: fail
string = 000:2
expected ouput: fail
Run Code Online (Sandbox Code Playgroud)

Riz*_*man 5

你可以试试这个:

^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$
Run Code Online (Sandbox Code Playgroud)

演示