Regular expression for three or more consecutive letters of the alphabet

556 Views Asked by At

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?

1

There are 1 best solutions below

1
JvdV On

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.