简体   繁体   English

正则表达式以匹配具有不同数字和最小长度的数字

[英]Regex to match number with different digits and minimum length

I am trying to write a regex (to validate a property on ac# .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. 我正在尝试编写一个正则表达式(以验证ac#.NET Core模型上的属性,该模型会生成javascript表达式)以匹配由至少两个不同数字和最小6位数字组成的所有数字。

For example: 例如:

222222 - not valid 222222-无效

122222 - valid 122222-有效

1111125 - valid 1111125-有效

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,} ? 我正在尝试以下表达式: (\\d)+((?!\\1)(\\d)) ,如果数字不同则匹配序列,但如何将整个模式的大小限制为{6,}

Many thanks 非常感谢

You may use 您可以使用

^(?=\d{6})(\d)\1*(?!\1)\d+$

See the regex demo 正则表达式演示

Details 细节

  • ^ - start of string ^ -字符串开头
  • (?=\\d{6}) - at least 6 digits (?=\\d{6}) -至少6位数字
  • (\\d) - any digit is captured into Group 1 (\\d) -任何数字都被捕获到组1中
  • \\1* - zero or more occurrences of the value captured in Group 1 \\1* -组1中捕获的值出现零次或多次
  • (?!\\1) - the next digit cannot be the same as in Group 1 (?!\\1) -下一位不能与组1中的相同
  • \\d+ - 1+digits \\d+ -1个数字
  • $ - end of string. $ -字符串结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM