三个或多个连续字母的正则表达式

发布于 2025-02-10 18:06:16 字数 837 浏览 0 评论 0原文

我想编写一个正则表达式,该表达式会找到三个或更多连续字母的字母的实例。我打算将此正则发行与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 - match abc
  • def - match def
  • lmnop - match lmnop
  • xxxxghixxxx - match ghi
  • ab - no match (not long enough)
  • zyx - no match (not in order)
  • q r s - no match (interceding chararacters)
  • tuwx - no match (missing v)

Is there a way to write this regex that doesn't use 20+ levels of nested parenthesis and 20+ |s?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑凤梨 2025-02-17 18:06:16

这可以缩短为:

(?:(?=ab|bc|cd|de|ef|fg|gh|hi|ij|jk|kl|lm|mn|no|op|pq|qr|rs|st|tu|uv|vw|wx|xy|yz).){2,}.

在线查看 demo


  • (:) - 打开一个非捕捉组;
    • (?= ab | bc | cd ...)。 - 嵌套的正面lookahead to sossert to noveAhead具有正确的继任者;
  • ){2,} - 关闭非捕捉组并匹配2次;
  • - 匹配最终字符以结束子字符串。

This can be shortened to:

(?:(?=ab|bc|cd|de|ef|fg|gh|hi|ij|jk|kl|lm|mn|no|op|pq|qr|rs|st|tu|uv|vw|wx|xy|yz).){2,}.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文