Search code examples
textnotepad++prefixes

How can I add the same prefix to all instances of a list of words in Notepad++?


I have a large block of text, containing one or more instances of each of a list of words. I need to add a prefix to all instances of each word on the list.

Here's an example of what I'm trying to do -

List of words:

foo
bar

Prefix:

xyz

Text before:

onklnagknaubosabdof foo dklfn fknk foo klnksdnia bar dsknska foo knkn bar

Text after:

onklnagknaubosabdof xyzfoo dklfn fknk xyzfoo klnksdnia xyzbar dsknska xyzfoo knkn xyzbar

How can I do this in Notepad++? Or is there another tool that can do this for me easily?


Solution

    • Ctrl+H
    • Find what: (?=\b(?:foo|bar)\b)
    • Replace with: xyz
    • UNcheck Match case
    • check Wrap around
    • check Regular expression
    • Replace all

    Explanation:

    (?=         # start lookahead, make sure we have after:
      \b        # word boundary, to NOT match xyzfoo or foobar
      (?:       # start non capture group
        foo     # literally
       |        # OR
        bar     # literally
      )         # end group
      \b        # word boundary, to NOT match xyzfoo or foobar
    )           #end lookahead
    

    Result for given example:

    onklnagknaubosabdof xyzfoo dklfn fknk xyzfoo klnksdnia xyzbar dsknska xyzfoo knkn xyzbar
    

    Screen capture:

    enter image description here

    If you want to replace foobar with xyzfooxyzbar, remove the word boundaries:

    • Find what: (?=(?:foo|bar))