1

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>
logi-kal
  • 7,107
  • 6
  • 31
  • 43

2 Answers2

2

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

  • Ctrl+H
  • Find what: <.+?>(*SKIP)(*FAIL)|.+?
  • Replace with: LEAVE EMPTY
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • Replace all

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

Toto
  • 89,455
  • 62
  • 89
  • 125
1

Find:

(?m).+?(<.*?>|$)

Replace with:

$1

where:

  • (?m) is a flag activating the multiline mode
  • .+? searches for one or more characters (but as less as possible)
  • (<.*?>|$) matches the desired pattern or the end of the line

Screenshots

Before: Screenshot before

After: Screenshot after

logi-kal
  • 7,107
  • 6
  • 31
  • 43