regexnotepad++nsregularexpressionfind-replace

Replace same character on one line multiple times Notepad++


I have a many files and in each file there is a line with the following (amongst other things). This line is not always in the same place, however, it starts from the beginning of the line. This line is also always different.

slug: bláh-téxt-hello-write-sométhing-ábout-arrow

I'd like to replace each occurrence of the special characters (á and é with their corresponding characters a and e). Each of those characters can be on this line many times + also occur in the document in other places (which should not be replaced).

So the result is:

slug: blah-text-hello-write-something-about-arrow

I have this: find: ^slug: (.)((é)|(á))(.) replace: slug: $1(?2e)(?2a)$3

However, this seems to replace only one character at a time. How do I get it to run multiple times until there is no character to replace?

Thanks much for any insights.


Solution

  • You can use

    Find What: (?:\G(?!^)|^slug:\h*)[^\r\náé]*\K(?:(á)|é)
    Or
    Find What: (?:\G(?!^)|^slug:\h*).*?\K(?:(á)|é)
    Replace With: (?1a:e)

    Details:

    In the replacement, (?1a:e), replaces the found match with a if Group 1 matched, else, e is used.

    See the regex demo.

    enter image description here

    Extra information about the use of conditional replacement is available in my "Swapping multiple values using conditional replacement patterns in Notepad++" YT video.

    Extra information about the use of \G operator can be found in another YT video of mine, "\G anchor use cases".