If one uses a regex engine that supports \G
and \K
, the following regular expression could be used.
^(?=.*\bKeyWord2\b)|\G.*?\K\bKEYWORD1\b
with the case-indifferent flag and, depending on requirements, multiline flag, set.
PCRE demo
With PCRE (PHP) and some other regex engines the anchor \G
matches the end of previous match. For the first match attempt, \G is equivalent to \A
, matching the start of the string. See this discussion for details.
\K
resets the starting point of the reported match to the current position of the engine's internal string pointer. Any previously consumed characters are not included in the final match. In effect, \K
causes the engine to "forget" everything matched up to that point. Details can be found here.
As shown at the link, there are four matches of the string
The KEYWORD1 before KeyWord2 then KEYWORD1 and KEYWORD1 again
They are an empty string at the beginning of the string and each of the three instances of KEYWORD1
. In fact for every string matched one of the matches will be an empty string at the beginning of the string. Empty strings must therefore be disregarded when making substitutions.