4

如果没有字符重复 4 次或更多次,我想构建将匹配密码的正则表达式。

我想出了正则表达式,如果有字符或字符组重复 4 次,它将匹配:

(?:([a-zA-Z\d]{1,})\1\1\1)

有没有办法仅在字符串不包含重复项时才进行匹配?我尝试了 正则表达式中建议的方法来匹配不包含单词的行?因为我认为正/负前瞻的某种组合会成功。但我还没有找到工作的例子。

重复是指字符串中任意位置的任意数量的字符

示例 - 不应匹配

aaaaxbc

阿里巴巴

x14aaaabc

示例 - 应该匹配

阿布卡哈兹

(a 在这里 4 次但没问题,我想过滤掉重复的模式)

4

2 回答 2

1

该链接非常有帮助,我可以使用它从您的原始表达式创建正则表达式。

^(?:(?!(?<char>[a-zA-Z\d]+)\k<char>{3,}).)+$ 

或者

^(?:(?!([a-zA-Z\d]+)\1{3,}).)+$ 

雷伊

于 2013-12-08T15:25:11.213 回答
0

Nota Bene: this solution doesn't answer exaactly to the question, it does too much relatively to the expressed need.

-----

In Python language:

import re

pat = '(?:(.)(?!.*?\\1.*?\\1.*?\\1.*\Z))+\Z'

regx = re.compile(pat)

for s in (':1*2-3=4@',
          ':1*1-3=4@5',
          ':1*1-1=4@5!6',
          ':1*1-1=1@',
          ':1*2-a=14#a~7&1{g}1'):
    m = regx.match(s)
    if m:
        print m.group()
    else:
        print '--No match--'

result

:1*2-3=4@
:1*1-3=4@5
:1*1-1=4@5!6
--No match--
--No match--

It will give a lot of work to the regex motor because the principle of the pattern is that for each character of the string it runs through, it must verify that the current character isn't found three other times in the remaining sequence of characters that follow the current character.
But it works, apparently.

于 2013-12-08T16:20:21.843 回答