问题描述
谁能帮助我或指导我构建一个正则表达式来验证重复数字
Can anyone help me or direct me to build a regex to validate repeating numbers
例如:11111111、2222、99999999999等
eg : 11111111, 2222, 99999999999, etc
它应该验证任何长度.
推荐答案
\b(\d)\1+\b
说明:
\b # match word boundary
(\d) # match digit remember it
\1+ # match one or more instances of the previously matched digit
\b # match word boundary
如果 1
也应该是有效匹配(零重复),请使用 *
而不是 +
.
If 1
should also be a valid match (zero repetitions), use a *
instead of the +
.
如果你还想允许更长的重复(123123123
)使用
If you also want to allow longer repeats (123123123
) use
\b(\d+)\1+\b
如果应该将正则表达式应用于整个字符串(而不是在更长的字符串中查找重复数字"),请使用行首和行尾锚点而不是 \b
:
If the regex should be applied to the entire string (as opposed to finding "repeat-numbers in a longer string), use start- and end-of-line anchors instead of \b
:
^(\d)\1+$
如何匹配完全相反,我.e.并非所有数字都相同的数字(除非整个数字只是一个数字):
How to match the exact opposite, i. e. a number where not all digits are the same (except if the entire number is simply a digit):
^(\d)(?!\1+$)\d*$
^ # Start of string
(\d) # Match a digit
(?! # Assert that the following doesn't match:
\1+ # one or more repetitions of the previously matched digit
$ # until the end of the string
) # End of lookahead assertion
\d* # Match zero or more digits
$ # until the end of the string
这篇关于正则表达式查找重复数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!