正则表达式查找具有唯一数字的数字 [英] regex to find numbers with unique digits

查看:327
本文介绍了正则表达式查找具有唯一数字的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到没有重复数字的10位数字,例如:

I want to find 10 digit numbers with no repeat digits, for example:

1123456789 //fail, there are two 1's
6758951230 //fail, there are two 5's
6789012345 //pass, each digit occurs once only. 

目前我正在使用正则表达式,但只能匹配10位数字(它不会检查重复项。我我正在使用这个正则表达式:

at the moment I am using regex but can only match 10digits numbers(it doesnt check for duplicates. I am using this regex:

[0-9]{10}

这可以用正则表达式完成还是有更好的方法来实现这个目标?

Can this be done with regex or is there a better way to achieve this?

推荐答案

这个正则表达式有效:

^(?!.*(.).*\1)\d{10}$

这使用锚定的负向前看,后面引用断言没有重复的字符。

This uses an anchored negative look ahead with a back reference to assert that there are no repeating characters.

查看使用您的现场演示示例。

在java中:

if (str.matches("^(?!.*(.).*\\1)\\d{10}"))
    // number passes

这篇关于正则表达式查找具有唯一数字的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆