How to replace "[abc] , [acb] , [bca]" with "[aaa]" in one go in Notepad ++

Viewed 61

Is there a way to Replace all of these texts in one go in Notepad++

[Washington]
...
[Henry]
...
[James]
...

so that it will be like this:

<span style="color:black">[Washington]</span>
...
<span style="color:black">[Henry]</span>
...
<span style="color:black">[James]</span>
...

What I'm thinking is that maybe there can be a substitute inside the brackets?

Like

[substitute] replaced by [new text]

Really need to do this in one go since there's a lot of these type on what I'm working on (a hobby)

Will appreciate it!

2 Answers

How to do it

In the Find and Replace window:

  1. Choose the "Replace" tab
  2. Tick "Regular expression" down at the bottom
  3. Set the "Find what" to ^(\[.+\])$
  4. Set the "Replace with" to <span style="color:black">\1</span>
  5. Click "Replace All"

How it works

How we find the stuff we want to replace:

  • ^ $ means the thing we match must take the whole line
  • ( ) means the thing we match will be assigned to \1
  • \[ \] means the thing we match must be surrounded by square brackets
  • .+ means anything can be inside the square brackets

Then we replace it with some stuff and use \1 wherever the matched stuff should appear.

Your actual question

The stuff above makes your example about putting HTML outside the square brackets work but it doesn't answer the question about replacing the stuff inside the square brackets.

[substitute] replaced by [new text]

To do this you are replacing with fixed text so you don't need the backreference:

  1. Choose the "Replace" tab
  2. Tick "Regular expression" down at the bottom
  3. Set the "Find what" to ^\[.+\]$
  4. Set the "Replace with" to [new text]
  5. Click "Replace All"

Make sure regular expression is selected.

You could use a negated character class to match from an opening till closing square bracket.

In the replacement use the full match $0

Find what:

\[[^][]*\]

See a regex demo

Replace with:

<span style="color:black">$0</span>

Output

<span style="color:black">[Washington]</span>
...
<span style="color:black">[Henry]</span>
...
<span style="color:black">[James]</span>
Related