I have a test string
"apple search from here apple, banana, apple."
and the following RegEx
(?i)(?<=search from here\s)(\bapple|banana|orange\b)(\s+(\bapple|banana|orange\b))*
I'm getting a match only for the first occurrence of apple. See https://regex101.com/r/EQin6O/1
How do I get matches for each occurrence of apple after the "search from here" text?
That should do the job:
(?:\G(?!\A)|search from here ).*?\K(apple|banana|orange)
See this https://regex101.com/r/q3FGoD/1
Step by step:
\G
- asserts we are at the beginning of the previous match or start of the string(?!\A)
- negative lookahead for the start of the String - that help us to omit start of the String in \G
|search from here
- alternatively look for string search from here
- that provides us the first match.*?
- allows for any characters in between the search from here
and a captured group (apple|banana|orange)
\K
omit previous matches(apple|banana|orange)
- eventually captures the matches matching alternatively one of given words