regex to find numbers with unique digits

前端 未结 5 775
忘了有多久
忘了有多久 2020-12-10 13:19

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 //         


        
5条回答
  •  臣服心动
    2020-12-10 14:15

    lancemanfv regex reference https://stackoverflow.com/a/12870549/1366360 is a great one, but the suggested regex is slightly off.

    Instead try

    ^(?:([0-9])(?!.*\1)){10}$ 
    

    This will match any string that begins and ends with 10 digits that are all different.

    If you want to check (and extract) if a longer string contains a 10 digit number with each number different use this

    ((?:([0-9])(?!.*\2)){10})*
    

    You can then use a numbered reference to extract the matching number

提交回复
热议问题