我目前正在尝试匹配重复的数字,到目前为止,我已经得到了这个:
pattern = /(\d){2}/
Run Code Online (Sandbox Code Playgroud)
但是当我用任意长度> = 2的数量测试这个模式时,它将返回true.我想要找到的是:当我测试数字12344时,它应该返回true,如果数字是12345,它应该返回false.但是拥有12444的数字也应该返回false.我想找到重复两次相同的数字.
编辑:感谢任何人提出解决方案!
对于这种任务,您必须使用外观和反向引用:
(?:^|(.)(?!\1))(\d)\2(?!\2)
Run Code Online (Sandbox Code Playgroud)
说明:
(?: // match either...
^ // start of the string
| // or...
(.) // any character
(?!\1) // not followed by the exact same character
)
(\d) // then, match and capture a digit
\2 // and the same digit a 2nd time
(?!\2) // and assert the digit doesn't show up a 3rd time
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
765 次 |
| 最近记录: |