Search code examples
regexregex-lookarounds

Regex : I need to extract all words except the string `ignore`


Take this as an example

hello-world
ignore-good-morning-ignore
good-night-ignore

I tried negative look ahead and negative look behinds

^(?<!ignore)\S+(?!ignore)$

But it seems to pick the whole 3 words

Expected result

hello-world
good-morning
good-night

Actual result

hello-world
ignore-good-morning-ignore
good-night-ignore

Solution

  • I'd suggest to swap the logic and actually match the word you'd like to ignore and replace those values leaving the string in the expected output. For example:

    (?<=\S)-ignore\b|\bignore-(?=\S)
    

    See an online demo. You can see the results in the bottom of the screen when we replace matched substring with nothing.


    • (?<=\S) - Postive lookbehind to assert position is preceded by a non-whitespace character.
    • -ignore\b - Match '-ignore' followed by a word-boundary.
    • | - Or:
    • \bignore- - Match a word-boundary followed by 'ignore-'.
    • (?=\S) - Positive lookahead to assert position is followed by a non-whitespace character.

    Note, if your string can also just be 'ignore' without anything else, you could just add to the alternation to capture that too.