Search code examples
regexnotepad++

Add to end of line that contains a specific word and starts with x


I would like to add some custom text to the end of all lines in my document opened in Notepad++ that start with 10 and contain a specific word (for example "frog").

So far, I managed to solve the first part.

Search: ^(10)$

Replace: \1;Batteries (to add ;Batteries to the end of the line)

What I need now is to edit this regex pattern to recognize only those lines that also contain a specific word.

For example:

Before: 1050;There is this frog in the lake

After: 1050;There is this frog in the lake;Batteries


Solution

  • You should allow any characters between the number and the end of line:

    ^10.*frog.*

    And replacement will be $0;Batteries. You do not even need a $ anchor as .* matches till the end of a line since . matches any character but a line break char.

    NOTE: There is no need to wrap the whole pattern with capturing parentheses, the $0 placeholder refers to the whole match value.

    More details:

    • ^ - start of a line
    • 10 - a literal 10 text
    • .* - zero or more chars other than line break chars as many as possible
    • frog - a literal string
    • .* - zero or more chars other than line break chars as many as possible