notepad++regex-replace

regular expression to remove all other words except the words between special characters in notepad++


I am trying to create a regex in Notepad++ to remove words except those enclosed between special characters. I am using this regex \<.*?\> which removes the words along with text.

Eg:
Sample text

random text <ABCD> random text
random text <QWERT> random text
random text <XYZ> random text

Output

random text random text
random text random text
random text random text

I just want the opposite of the above regex

Eg:
Sample text

random text <ABCD> random text
random text <QWERT> random text
random text <XYZ> random text

Output

<ABCD>
<QWERT>
<XYZ>

Solution

  • This is a job for (*SKIP)(*FAIL) verbs.

    Explanation:

    <.+?>       # matches the string to be kept
    (*SKIP)     # skip this match
    (*FAIL)     # considere it failed
      |           # OR
    .+?         # match any character but newline
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here