问题
I am trying to write a regex (to validate a property on a c# .NET Core model, which generates javascript expression) to match all numbers composed by at least two different digits and a minimum length of 6 digits.
For example:
222222 - not valid
122222 - valid
1111125 - valid
I was trying the following expression: (\d)+((?!\1)(\d))
, which matches the sequence if has different digits but how can I constrain the size of the whole pattern to {6,}
?
Many thanks
回答1:
You may use
^(?=\d{6})(\d)\1*(?!\1)\d+$
See the regex demo
Details
^
- start of string(?=\d{6})
- at least 6 digits(\d)
- any digit is captured into Group 1\1*
- zero or more occurrences of the value captured in Group 1(?!\1)
- the next digit cannot be the same as in Group 1\d+
- 1+digits$
- end of string.
来源:https://stackoverflow.com/questions/47402863/regex-to-match-number-with-different-digits-and-minimum-length