Search code examples
regextextregex-negationmultilineregexp-replace

Select everything expect a word with spaces using regex without multiline


I try to filter a text with a regex. The program I use can only replace text with a regex pattern. So I have to generate a regex that will select everything except a specific pattern/word with spaces and I will replace it with nothing (empty). Unfortunately, the program only uses regex without the multiline option

Goal:

  • filter a text to get specific words

My limitations:

  • only regex with replace available
  • no multiline

Example Text:

ABC 11
ABC 1/10
ABC 1/20
ABC 1/20
ABC 2/10
ABC 2/20
ABC 2/30
ABC 2/40

FULLTEXT
Other Full Text

I want to select everything expect:

ABC 11
ABC 2/10
ABC 2/20
ABC 2/30
ABC 2/40

The pattern with multiline looks like this:

/^((?!ABC 11|ABC 2\/[0-9][0-9]).)*$/gm

https://regex101.com/r/l2zvMX/1

But I search for one without the "/gm". Only "/g"

Best regards, ElGammler


Solution

  • You can use

    (?m)^(?:(?!ABC 11|ABC 2\/[0-9][0-9]).)*$
    

    Details:

    • (?m) - a multiline flag that makes ^ match start of any line and $ to match any line end

    • ^ - line start

    • (?:(?!ABC 11|ABC 2\/[0-9][0-9]).)* - any one char, zero or more repetitions, as many as possible, that does not start the ABC 11 char sequence or ABC 2\/[0-9][0-9] pattern: ABC 2/ and then any two digits

    • $ - line end.