我用Python学习了很多正则表达式,我很难用正则表达式来支持没有连续4个或更多重复字符的测试用例,我在互联网上找到了简单的解决方案,但我需要判断不包括"-"字符的重复字符,例如:
bic-hota // Valid
bichota // Valid
bichota1234 // Valid
bich222ota // Valid, sequence "222" with length of 3 is allowed
bichota2222 // Invalid because have "2222" and isn't allowed length of 4 or more
bichota22-22 // Invalid because have "2222" (ignore the '-') and isn't allowed length of 4 or more
在最后一个测试用例中,您可以看到序列"22-22",我需要忽略序列之间的"-",以验证整个序列,我已经搜索了如何解决这个问题,我使用了非捕获组(?:-?)
,但这不起作用:(
^(?![\w]*([\w])(?:-?)\1{3,})[\w-]+$
我的正则表达式有什么问题,如何修复?