生成正则表达式

时间:2013-01-29 13:35:54

标签: regex numbers

任何人都可以帮我创建一个正则表达式,可以检查数字是否有2和4个数字重复,比如如何检查这个没有5555122

我想通过正则表达式知道第四双重复。

2 个答案:

答案 0 :(得分:0)

您可以使用反向引用

(\d)\1{3}

这将检查4位数的重复

答案 1 :(得分:0)

试试这个

(\d)(?:\1{3}|\1)

<强>解释

"
(           # Match the regular expression below and capture its match into backreference number 1
   \\d          # Match a single digit 0..9
)
(?:         # Match the regular expression below
               # Match either the regular expression below (attempting the next alternative only if this one fails)
      \\1          # Match the same text as most recently matched by capturing group number 1
         {3}         # Exactly 3 times
   |           # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      \\1          # Match the same text as most recently matched by capturing group number 1
)
"

修改

(?:\1{3}|\1)内的模式,需要更多关注。如果它与(?:\1|\1{3})类似,则与预期不符。因为,The Regex-Directed Engine Always Returns the Leftmost Match