我想找到没有重复数字的 10 位数字,例如:
1123456789 //fail, there are two 1's
6758951230 //fail, there are two 5's
6789012345 //pass, each digit occurs once only.
目前我正在使用正则表达式,但只能匹配 10 位数字(它不检查重复项。我正在使用这个正则表达式:
[0-9]{10}
这可以用正则表达式完成还是有更好的方法来实现?
最佳答案
这个正则表达式有效:
^(?!.*(.).*\1)\d{10}$
这使用 anchor 定的否定前瞻和后向引用来断言没有重复字符。
见 live demo使用您的示例。
在Java中:
if (str.matches("^(?!.*(.).*\\1)\\d{10}"))
// number passes
关于java - 正则表达式查找具有唯一数字的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63402284/