Notepad++ Search and Replace From List With Limited Replacements

  • I am looking for a way to search and replace a word from a list.

    I have 380 words that are “city” I want to place only 4 of the city words with “Sydney” and then move down my document and replace another 4 “city” words with “Melbourne”.

    Keep going down my document until all “city” words are replaced with my list of words.

    What would be the best way to achieve this?


  • @Meathead ,

    What would be the best way to achieve this?

    My first reaction would be “use one of the scripting plugins, like PythonScript”

    There are examples in the forum of doing something similar using just regexes, by putting your list of words at the end of the document in a particular format, but that solution gets fragile if you’ve got a long document.

    If you’ve got a reasonably-short document (<1000 lines estimated), then can help you go down the regex route. If the document is longer, or if the list of cities requires more than about 1000 characters to list, then regex is not the right way, and we can help you with the script solution as well…

    However, before we can help, we would need a better indication of your data. Do an abbreviated version of your city list, like “Sydney, Melbourne, Adelaide”, and format up a short-but-representative block of text, which you will show using the </> button, so it ends up in a box like:


    (If you have private data, share dummy values… as long as it has the same structure and “look”, it doesn’t have to be your real data)

    Show this block of text twice. Once in the original “before” state, where it has the “city” dummy text. The second time in the “after” state, with the replacements made. That way, we can see if we understand how your brief 2-sentence description matches your expected “before” and “after”.

    After following this advice, and letting us know the approximate size of both your document and the number of replacement cities, we might be able to give more directed help.

    I am also including my generic search/replace boilerplate, which reinforces some of what I’ve already said.

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.

  • @Meathead

    It’s really a rather easy thing for a scripting plugin to do, as Peter says.

    Here’s a demo using PythonScript; see if you can follow its logic without further explanation:

    from Npp import editor
    city_string = '''
    city_list = city_string.splitlines()
    for city in city_list: 
        editor.replace('city', city, 0, 0, editor.getLength(), 4)

Log in to reply