我正在努力编写正则表达式模式。我想要It must NOT have or more consecutive repeated digits.
5133-3367-8912-3456
so it should return false, Consecutive digits are repeating 4 or more times
5123-4567-8912-3456
and it should return True
我现在的代码:re.match('[0-9]{4}', string)
我可以在python中用很多方法,但我想用regex,因为我的情况非常不同。
在这种情况下,有人能帮我吗?
您可以在python中使用此regex并进行负面展望:
^(?![d-]*(d)(?:-?1){3})d+(?:-d+)*$
RegEx演示
RegEx详细信息:
^
:启动(?![d-]*(d)(?:-?1){3})
:当我们发现相同数字的4个重复(可选地前面有一个-
(时,负前瞻将使匹配失败d+(?:-d+)*
:匹配由-
分隔的数字集$
:结束
如果首先删除所有连字符,则可以使用正向先行搜索指定次数的重复模式。
if re.search(r"(d)(?=1{3})", string):
# Successful match -> This would be your false case
else:
# Match attempt failed -> This would be your true case