regexregex-lookaroundscapturing-grouppositive-lookbehind

Regex: Only the first match of word after a given word is returned


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?


Solution

  • 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: