正则表达式重复字符计数 [英] regex repeated character count
本文介绍了正则表达式重复字符计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一组字符,如abcdefghij,并使用这些字符,我使用这些字符随机生成一个密码。生成的密码可以有例如6个字符。如何使用regex验证密码,以使邻居字符不相同,且字符不会重复两次以上?
解决方案
你可以使用类似的:
/ ^
(?:(。)
\\ 1)#邻居字符不相同
(?!(?>。*?\1){2})#字符不会超过两次
)*
\\ \\ z / x
Perl引用,如果不支持原子组可以删除。
在Java regex中,它可以写成:
^(?:(。)(?! \1 |(?:。*?\1){2}))* \z
/ pre>
If I have a set of characters like "abcdefghij" and using this characters, I generate random a password using this characters. A generated password can have, for example, 6 characters. How to validate a password using regex so that tho neighbor characters are not identical and a character does not repeat more that twice?
解决方案You could use something like:
/^ (?:(.) (?!\1) # neighbor characters are not identical (?!(?>.*?\1){2}) # character does not occur more than twice )* \z/x
Perl quoting, the atomic group can be removed if not supported.
In Java regex it could be written like:
^(?:(.)(?!\1|(?:.*?\1){2}))*\z
这篇关于正则表达式重复字符计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文