三个或多个连续字母的正则表达式
我想编写一个正则表达式,该表达式会找到三个或更多连续字母的字母的实例。我打算将此正则发行与JavaScript和GREP
一起使用。这似乎是Regex应该可以轻松地做的事情,但是我很难想到的是不太复杂的正则:
(abc(d(e(f(f(g(h(h(h(h(h( i(j(k(l)(n(n(o)(p(q(r),s(s(t(w(x(yz?)?)?)?)?)?)?)?)?)? )?)?)?)?)?)?)?)?)?)?)?)?)|(bcd(e(f(g) (n(o(p(q)(r(s(t(u(v(w(x(yz?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)? ?)?)?)?)?)?)?)?)|(cde(f(g) t(u(v(w(x(yz?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)? ?
)
-
- 匹配
def
- lmnop
xxxxxxghixxxxx
- 匹配lmnop
- 匹配/code> - 没有匹配(不够长)
zyx
- 否匹配(不顺序)qr s
- no Match(Interceding Chararacters)tuwx
- 没有匹配(缺少v
)
有没有办法编写不使用20多个嵌套括号的层次和20+ |
s的正则表达式?
I want to write a regular expression that will find instances of three or more consecutive letters of the alphabet. I'm planning to use this regex with both JavaScript and grep
. This seems like something that regex should be able to do pretty easily, but I'm having a hard time coming up with a regex that isn't very complicated:
(abc(d(e(f(g(h(i(j(k(l(m(n(o(p(q(r(s(t(u(v(w(x(yz?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)|(bcd(e(f(g(h(i(j(k(l(m(n(o(p(q(r(s(t(u(v(w(x(yz?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)|(cde(f(g(h(i(j(k(l(m(n(o(p(q(r(s(t(u(v(w(x(yz?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)|...
To be clear, I want to match these test cases:
abc
- matchabc
def
- matchdef
lmnop
- matchlmnop
xxxxghixxxx
- matchghi
ab
- no match (not long enough)zyx
- no match (not in order)q r s
- no match (interceding chararacters)tuwx
- no match (missingv
)
Is there a way to write this regex that doesn't use 20+ levels of nested parenthesis and 20+ |
s?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可以缩短为:
在线查看 demo
(:
) - 打开一个非捕捉组;(?= ab | bc | cd ...)。
- 嵌套的正面lookahead to sossert to noveAhead具有正确的继任者;){2,}
- 关闭非捕捉组并匹配2次;。
- 匹配最终字符以结束子字符串。This can be shortened to:
See an online demo
(?:
- Open a non-capture group;(?=ab|bc|cd...).
- A nested positive lookahead to assert character after lookahead has it's correct successor;){2,}
- Close non-capture group and match 2+ times;.
- Match the final character to conclude the substring.