Regex to match number with different digits and minimum length

*爱你&永不变心* 提交于 2021-01-27 14:24:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!