gpt4 book ai didi

java - 正则表达式查找具有唯一数字的数字

转载 作者:行者123 更新时间:2023-12-01 15:45:40 25 4
gpt4 key购买 nike

我想找到没有重复数字的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com