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- matchabcdef- matchdeflmnop- matchlmnopxxxxghixxxx- matchghiab- 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?
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.