正则表达式重复字符计数
如果我有一组像“abcdefghij”这样的字符并使用这些字符,我会使用这些字符随机生成一个密码。例如,生成的密码可以有 6 个字符。如何使用正则表达式验证密码,以使相邻字符不相同并且字符不会重复两次以上?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用类似:
Perl 引用,如果不支持,可以删除原子组。
在 Java 正则表达式中,它可以写成:
You could use something like:
Perl quoting, the atomic group can be removed if not supported.
In Java regex it could be written like:
AFAIK,这不能用简单的正则表达式来完成(特别是,确保一个字母最多只出现两次。你可以做一堆像
and 之类的表达式(匹配意味着验证失败):
但显然这不是一个好主意
。字符不重复在一起的情况也许可以用捕获组来处理,但我几乎肯定不能用正则表达式检查另一个
......为什么痴迷于正则表达式?编程这些检查是微不足道的,正则表达式在一组情况,但并非所有检查都可以使用正则表达式完成。
AFAIK, this cannot be done with a simple regexp (particularly, ensuring that a letter only appears twice at max. You could do a bunch of expressions like
and also (matching means the validation failed):
but obviously this is not good idea.
The condition that the characters do not repeat together maybe can treated with capturing groups, but I am almost sure the other one cannot be checked with regex.
BTW... why the obsession with regex? Programming these checks are trivial, regex is useful in a set of cases but not every check can be done with regex.