正则表达式匹配 3 个或更多连续连续字符和连续相同字符
我需要正则表达式来匹配以下情况。
- 3 个或更多连续的连续字符/数字;例如123、abc、789、pqr等
- 3个或更多连续相同的字符/数字;例如 111、aaa、bbb、222 等。
I need regular expressions to match the below cases.
- 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.
- 3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我认为您不能(轻松)在第一种情况下使用正则表达式。第二种情况很简单:
由于
\\1
表示与组1
匹配的部分,这将匹配位于az 范围内的三个相同字符的任何序列
或数字 (\d
)。更新
需要明确的是,对于第一种情况,您可以使用正则表达式。然而,这种模式是如此费力且极其复杂,所以你最好根本不这样做。特别是如果你想真正涵盖所有字母表。在这种情况下,您可能应该通过迭代 Unicode 字符集的字符代码或类似的东西以编程方式生成模式,并为每三个连续字符生成分组。但是,您应该意识到,通过为模式匹配器生成如此大的决策树,行进性能必然会受到影响(
O(n)
,其中n
是数字组的大小是 Unicode 字符集的大小减 2)。I don't think you can (easily) use regex for the first case. The second case is easy though:
Since
\\1
represents part matched by group1
this will match any sequence of three identical characters that are either within the rangea-z
or are digits (\d
).Update
To be clear, you can use regex for the first case. However, the pattern is so laborious and ridiculously convoluted that you are better off not doing it at all. Especially if you wanted to REALLY cover all the alphabet. In that case you should probably generate the pattern programmatically by iterating the char codes of the Unicode charset or something like that and generate groupings for every three consecutive characters. However, you should realize that by having generated such a large decision tree for the pattern matcher, the marching performance is bound to suffer (
O(n)
wheren
is the number of groups which is the size of the Unicode charset minus 2).我不同意,情况 1 可以用于正则表达式,但你必须告诉它要匹配的序列...这有点长而无聊:
http://regexr.com/3dqln
I disagree, case 1 is possible to regex, but you have to tell it the sequences to match... which is kind of long and boring:
http://regexr.com/3dqln
对于第二个问题:
解释:
for the second question:
explanation:
据我所知,第一种情况确实不可能。正则表达式引擎不知道任何关于自然数或字母表的顺序。但至少可以区分 3 个或更多数字和 3 个或更多字母,例如:
这匹配
abcd
、ABCDE
或123
但例如,与ab2d
、A5c4
或12z
不匹配。据此,第二种情况可以用较短的版本正确给出:To my knowledge, the first case is indeed not possible. The regex engine doesn't know anything about the order of the natural numbers or the alphabet. But it's at least possible to differentiate between 3 or more numbers and 3 or more letters, for example:
This matches
abcd
,ABCDE
or123
but doesn't matchab2d
,A5c4
or12z
, for example. According to this, the second case can be correctly given in a shorter version as:正则表达式不可能。
使用模式
(?i)(?:([a-z0-9])\\1{2,})*
。如果要检查整个字符串,请使用
Matcher.matches()
。要查找字符串中的匹配项,请使用Matcher.find()
。这是一些示例代码:
输出是:
Not possible with regular expressions.
Use a pattern of
(?i)(?:([a-z0-9])\\1{2,})*
.If you want to check the whole string, use
Matcher.matches()
. To find matches within a string, useMatcher.find()
.Here's some sample code:
And the output is:
感谢大家对我的帮助。
对于第一种情况 - 3 个或更多连续的连续字符/数字;例如123、abc、789、pqr等。我使用了下面的代码逻辑。请分享您对此的评论。
Thanks All for helping me.
For the first case - 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc. I used below code logic. Pls share your comments on this.
匹配三个连续数字或字母的正则表达式是
“
([0-9]|[aA-zZ])\1\1
”Regex to match three consecutive numbers or alphabets is
"
([0-9]|[aA-zZ])\1\1
"如果您有下限 (3) 和上限 regexString 可以生成如下
输出
If you have lower bound (3) and upper bound regexString can be generated as follows
Output
全部放在一起:
3 个或更多连续的连续字符/数字;例如 123、abc、789、pqr 等。
3个或更多连续相同字符/数字;例如 111、aaa、bbb、222 等。
https://regexr.com/4727n
这也有效:
https://regex101.com/r/6fXC9u/1
All put together:
3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.
3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.
https://regexr.com/4727n
This also works:
https://regex101.com/r/6fXC9u/1
对于第一个问题,如果您可以使用较少的正则表达式,则此方法有效
for the first question this works if you're ok with less regex
3个或更多连续的连续字符/数字;例如123、abc、789、pqr等
<预><代码>(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(? =6)|6(?=7)|7(?= 8)|8(?=9)){2,}\d|(?:a(?=b)|b(?=c)|c(?=d)|d(?=e)|e( ?=f)|f(?=g)|g(?=h)|h (?=i)|i(?=j)|j(?=k)|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o( ?=p)|p(?=q)|q(?=r)|r (?=s)|s(?=t)|t(?=u)|u(?=v)|v(?=w)|w(?=x)|x(?=y)|y( ?=z)){2,}[\p{Alpha}])
https://regex101.com/r/5IragF/1
3个或更多连续的相同字符/数字;例如 111、aaa、bbb、222 等
<前><代码>([\p{Alnum}])\1{2,}
https://regex101.com/r/VEHoI9/1
https://regex101.com/r/5IragF/1
https://regex101.com/r/VEHoI9/1
全部放在一起:
3 个或更多连续的连续字符/数字;例如 123、abc、789、pqr 等。
3个或更多连续相同字符/数字;例如 111、aaa、bbb、222 等。
https://regexr.com/4727n
All put together:
3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.
3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.
https://regexr.com/4727n
对于案例 #2,我受到 regextester 上的示例的启发,并创建了以下正则表达式来匹配
n
个相同的数字(要检查数字和字母,请将0-9
替换为A-Za-z0-9
):For case #2 I got inspired by a sample on regextester and created the following regex to match
n
identical digits (to check for both numbers and letters replace0-9
withA-Za-z0-9
):我正在与一位同事讨论这个问题,我们认为我们对#1 有一个很好的解决方案。
要检查 abc 或 bcd 或 ... 或 012 或 123 甚至任意数量的连续字符,尝试:
此解决方案的好处是,如果您想要超过 3 个连续字符,请将
{2,}
增加到比您要检查的值少 1 的值。每个组中的
?:
可防止该组被捕获。I was discussing this with a coworker and we think we have a good solution for #1.
To check for abc or bcd or ... or 012 or 123 or even any number of sequential characters, try:
The nice thing about this solution is if you want more than 3 consecutive characters, increase the
{2,}
to be one less than what you want to check for.the
?:
in each group prevents the group from being captured.尝试这个来回答第一个问题。
如果在 arg 中找到 3 个连续的数字或字母,则返回 true
Try this for the first question.
returns true if it finds 3 consecutive numbers or alphabets in the arg