REGEX capture each n-letters words between two words of a sentence

You may use this regex with a lookahead and \G:

(?:\bSTART\b|(?!^)\G)\h+(?!END\b).*?\b(\w{3,})(?=.*?\bEND\b)

RegEx Demo

RegEx Details:

  • (?:\bSTART\b|(?!^)\G): Match word START or starting from end of previous match match 0 or more words separated by 1+ whitespace.
  • \G: asserts position at the end of the previous match or the start of the string for the first match
  • \h+(?!END\b).*?(\w{4,}): Match 1+ whitespace followed 0 or more characters followed by a word of 4+ length which is captured in group #1
  • (?=.*?\bEND\b): Lookahead to assert presence of word END ahead

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top