gpt4 book ai didi

用于验证字符串的python re

转载 作者:太空狗 更新时间:2023-10-30 01:57:01 27 4
gpt4 key购买 nike

代码如下:

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"

上面的代码是打印“pass”

我的预期输出应该是“失败”

下面是几个例子:

string = 00:20
expected output: pass
string = 00:202
expected ouput: fail
string = 00:2z
expected output: fail
string = 000:2
expected ouput: fail

最佳答案

你可以试试这个:

^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$

Demo

关于用于验证字符串的python re,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45158443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com