Notepad++ regex replace capture group examples list

You can check previous article on the topic:

Notepad++ regex replace wildcard capture group

In this example is shown how to format list of words from any words using Notepad++ regex to a simple or nested Java/Python list:

before

Animal	tiger, lion, mouse, cat, dog
Fish	shark, whale, cod
Insect	spider, fly, ant, butterfly

after

["Animal", ["tiger",  "lion",  "mouse",  "cat",  "dog"]],
["Fish", ["shark",  "whale",  "cod"]],
["Insect", ["spider",  "fly",  "ant",  "butterfly"]],

regex replace word by adding qoutes

  • Open the file in Notepad++
  • Replace CTRL + H
  • Add quotes to words(two times for the last/first column):
  • Find what - , (\w+)
  • Replace with - , "\1"
  • Find what - (\w+),
  • Replace with - "\1",

Before

Animal	tiger, lion, mouse, cat, dog
Fish	shark, whale, cod
Insect	spider, fly, ant, butterfly

After

Animal	"tiger",  "lion",  "mouse",  "cat",  "dog"
Fish	"shark",  "whale",  "cod"
Insect	"spider",  "fly",  "ant",  "butterfly"

Note that depending on the words (are there numbers, separator) different approaches could be taken. As a word regex capture group you can use simply: (\w+)

regex adding brackets

  • Open the file in Notepad++
  • Replace CTRL + H
  • Add quotes to words(two times for the last/first column):
  • Find what - (\w+)\t"
  • Replace with - ["\1", ["

Before

Animal	"tiger",  "lion",  "mouse",  "cat",  "dog"
Fish	"shark",  "whale",  "cod"
Insect	"spider",  "fly",  "ant",  "butterfly"

After

["Animal", ["tiger",  "lion",  "mouse",  "cat",  "dog"
["Fish", ["shark",  "whale",  "cod"
["Insect", ["spider",  "fly",  "ant",  "butterfly"

Make final list

At the last step we will add the final left brackets. Please note that we are not using group capture because the insert is more complex. In other words:

We are not using something like "\r\n -> \1]], but "\r\n and "]],\r\n . It can be done also by groups but it's more complicated.

My golden rule for regex is to be simple and easy to read.

  • Open the file in Notepad++
  • Replace CTRL + H
  • Add quotes to words(two times for the last/first column):
  • Find what - "\r\n
  • Replace with - "]],\r\n

Before

["Animal", ["tiger",  "lion",  "mouse",  "cat",  "dog"
["Fish", ["shark",  "whale",  "cod"
["Insect", ["spider",  "fly",  "ant",  "butterfly"

After

["Animal", ["tiger",  "lion",  "mouse",  "cat",  "dog"]],
["Fish", ["shark",  "whale",  "cod"]],
["Insect", ["spider",  "fly",  "ant",  "butterfly"]],

Related Article