4

I would like to use notepad++ to remove everything in a text file except anything found between [ ].

Example:

[D-1.1.1-A] - bla bla text here[D-1.1.1-B] - some more bla bla text here[D-1.1.1-C] - even more bla bla

would become:

[D-1.1.1-A][D-1.1.1-B][D-1.1.1-C]

3 Answers 3

7

What you're asking for can be more complicated than a regex if the input is more complex. However if the input is always as written, then

Ctrl-H
Find what: [^\]]+(\[|$)
Replace with: \1

should do it

1
  • 1
    yes, but this regex will delete also all the lines before the specific line with brackets :(
    – Just Me
    Feb 9, 2019 at 11:01
3

You can look for [^\[]*(\[.*?\])[^\[]*
(make sure "search mode" is "regular expression" and maybe check "wrap around")

And replace it to $1

An example.

1
  • How are $1 and /1 different?
    – neverMind9
    Jun 6, 2018 at 20:21
2
  • Ctrl+H
  • Find what: \[.*?\](*SKIP)(*FAIL)|.+?
  • Replace with: LEAVE EMPTY
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

  \[.*?\]       # 1 or more any charact, not greedy, between opening and closing square brackets
  (*SKIP)       # Special verb, forget it
  (*FAIL)       # Special verb, assume there is no match
|             # OR
  .+?           # 1 or more any character, not greedy

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .