正则表达式找到重复的数字

Cha*_*ila 28 regex

任何人都可以帮助我或指导我构建一个正则表达式来验证重复数字

例如:11111111,2222,99999999999等

它应该验证任何长度.

Tim*_*ker 49

\b(\d)\1+\b
Run Code Online (Sandbox Code Playgroud)

说明:

\b   # match word boundary
(\d) # match digit remember it
\1+  # match one or more instances of the previously matched digit
\b   # match word boundary
Run Code Online (Sandbox Code Playgroud)

如果1还应该是有效匹配(零重复),请使用a *而不是+.

如果你还想允许更长的重复(123123123)使用

\b(\d+)\1+\b
Run Code Online (Sandbox Code Playgroud)

如果正则表达式应该应用于整个字符串(而不是在更长的字符串中查找"重复数字"),请使用开始和结束行锚点而不是\b:

^(\d)\1+$
Run Code Online (Sandbox Code Playgroud)

编辑:如何匹配完全相反的,即不是所有数字都相同的数字(除非整个数字只是一个数字):

^(\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
Run Code Online (Sandbox Code Playgroud)


SLa*_*aks 10

要匹配单个数字的重复次数,您可以编写([0-9])\1*.

这匹配[0-9]到一个组,然后匹配该组的0或更多次重复(\1).

您可以编写\1+以匹配一个或多个重复.

  • 使用 `+` 而不是 `*`,否则将匹配单个数字。 (2认同)

mhy*_*itz 5

使用反向引用:

(\d)\1+
Run Code Online (Sandbox Code Playgroud)

可能你想使用某种锚^(\d)\1+$或者\b(\d)\1+\b


小智 5

我用这个表达式给了我所有相同数字的电话号码。

基本上,这意味着对给定数字的原始第一次重复进行 9 次重复,从而导致连续出现 10 次相同的数字。

([0-9])\1{9}