A common thing I need to do is insert text before and after a number of lines in a text editor. For example, I might have the following text:
test1 test2 test3 test4 test5
Which I want to change to something like this:
before_test1_after before_test2_after before_test3_after before_test4_after before_test5_after
So I’ve added the text before_ at the start of each line and the text _after at the end of the each line. To do this with Regex and Notepad++
- Open a new document, with the starting text.
- Choose Search > Replace (CTRL + H).
- Switch the search mode to Regular expression
To add text at the start of each line, search for ^(.) and replace with before_\1. This matches the start of the line and any character (.) after it. It replaces with the text before_ and the first match result \1.
Similarly to add text at the end of each line, search for (.)$ and replace with \1_after. This matches any character up to the end of the line. It replaces it with the search result followed by the text after_.