How to add new line of text after a specific text in Notpad++

command linenotepad

How to add new line of text after a specific text in Notepad++, while keeping the initial lines – lines always start with the same two words but the rest.

Alright, I have a text which contains multiple lines. I want to insert a new line after every line that starts with: 'source address' two words.
These two words might be used again within the text but I want to introduce new lines containing 'source port any' after each line that start with 'source address' words. These lines are longer and do Not always contain the same words but they always start with 'source address' words. I want to keep these lines but introduce a new line containing 'source port any' each time there's a line starting with 'source address'.

I've seen few close similar cases but please mind my current constraints: these lines are not always the same so when I use replace, I need to memorise/store the line which is not always the same but starts with same 2 words: 'source address'.

The regex expression fails to find 'source address'. I've tried: ^.source address.$ which does find the lines but it also matches when source address is not at the beginning of the lines – I only need to add a new line containing 'source port any' after the lines who start with 'source address' while these lines starting with source address two words need to remain – so when replace happens it needs to contain the previous line starting with source address. Also, this line contains more words but not always the same – only these two words source address is always the same.

Thank you in advanced.

Best Answer

Use Notepad++'s RegEx (Regular Expressions) find and replace functionality.

  • Find what: (^source address.*)
  • Replace with: \1\nsource port any
  • Ensure Regular Expressions is selected as the Search Mode.
  • Ensure . matches newline is not selected.

This will find any line starting with (^) "source address" and capture it (()). It will then replace that line with the captured text (\1), plus a newline marker (\n), plus "source port any".

Related Question