Home > Blockchain >  Find everything except the search match
Find everything except the search match

Time:07-30

I need to write a notepad regex to match everything besides my search criteria.

Fore example, if I have

James Bond (E1R1)

I have a regex to match E1R1. But I need to reverse it so I can get rid of everything besides E1R1.

So far I have ^(?!(?<=\(). ?(?=\))$).*$. But it seems to match everything.

CodePudding user response:

Use

^.*\(([^()\n\r]*)\)$|^(?!.*\(([^()\n\r]*)\)$).*\R?

Replace with $1.

See regex proof.

The expression finds lines ending with round brackets at the end, and removes all text outside those brackets. It will remove the entire line that contains no brackets at the end.

CodePudding user response:

You could match from an opening till closing parenthesis and skip that match. Then match any single character which should be replaced by an empty string.

\([^()\r\n]*\)(*SKIP)(*F)|.

Explanation

  • \([^()\r\n]*\) Match from an opening till closing parenthesis (....)
  • (*SKIP)(*F) Skip the match
  • | Or
  • . Match any character except a newline

Regex demo

  • Related