正则表达式以匹配具有不同数字和最小长度的数字 [英] Regex to match number with different digits and minimum length
本文介绍了正则表达式以匹配具有不同数字和最小长度的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个正则表达式(以验证ac#.NET Core模型上的属性,该模型会生成javascript表达式),以匹配由至少两个不同数字和最小6位数字组成的所有数字.
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.
例如:
222222-无效
122222-有效
1111125-有效
1111125 - valid
我正在尝试以下表达式:(\ d)+((?!\ 1)(\ d))
,如果数字不同,则匹配序列,但是如何限制大小整个模式为 {6,}
吗?
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{6})(\d)\1*(?!\1)\d+$
请参见 regex演示
详细信息
-
^
-字符串的开头 -
(?= \ d {6})
-至少6位数字 -
(\ d)
-任何数字都被捕获到第1组 -
\ 1 *
-组1中捕获的值出现零次或多次 -
(?!\ 1)
-下一位数字不能与第1组中的数字相同 -
\ d +
-1个以上数字 -
$
-字符串的结尾.
^
- 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.
这篇关于正则表达式以匹配具有不同数字和最小长度的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文