RegEX - Find and rename special words (letter)

  • How I can replace all special words in the TXT file with letter (-) to (_)?

    All replace words beginn with $:


    1. $this-is_my_test
    2. testfile\tmp$test-old_name -Value abc\no-wow

    My wish:
    testfile\tmp$test_old_name -Value abc\no_wow

    I have tested : $(.*)\
    this is not right because (.) and the space between two words are not found.
    I have no idea with regex. Sorry.

    Can anybody help me?

  • @Venus642 ,

    The first problem with your regex is the $ character: in Notepad++'s regular expressions, that means “end of line”. If you want to match the literal $ character in a regex, you need to escape it: \$.

    When solving a problem like yours, I try to state the problem in my native language in a way that encapsulates all of my requirements: “to be matched (and later replaced), the - must have the literal $ with one or more letter characters before it and must have one or more letter characters after it, but I only want the - to be replaced”.

    There are lots of ways to implement that sentence.

    First, I know there is going to be a - in my regex, because that’s the only character I want to really replace. I know that it needs to have something that “goes before”, and something that “goes after”, but I want the “goes before” and “goes after” to stay the same. The two ways I know of doing that are saving the “before” and “after” in groups, which can then be referenced in the replacement; or I can use the fancy features of look-behind or match-reset for the “before” and lookahead for the “after”.

    The groups are often easier to understand:

    • FIND = (?i)(\$\l+)-(\l+)
      • the (?i) looks like a group, but isn’t; instead, it makes sure that the match ignores case on letters
      • The other regex portions in parentheses are the groups for saving the “before” and “after”
      • \$\l+ means the literal $ followed by one or more lowercase letters – but because we told it to ignore case earlier, \l means any letter character. (Originally, I used \$\w+, which is literal $ followed by one or more “word characters”, which is uppercase, lowercase, or underscore. That may or may not work for you, since you never defined what characters were allowed in “replace words that begin with $”
      • the \l+ after the - captures the letters after the -
    • REPLACE = ${1}_${2}
      • that uses the contents of group1 (the letters before the -), then the literal _, then the contents of group2 (the letters after the -). Note that some might write that as $1_$2 or \1_\2, but those notation only allows groups 1 - 9; you have to include the {} if you want a group number bigger than that, or if you want to be able to put a literal digit after the match group.
    • MODE = regular expression

    Alternately, with match-reset and lookahead:

    • FIND = (?i)\$\l+\K-(?=\l+)
      • case insensitive, find literal $, find one or more letters, reset the match (so none of those will be replaced), find the literal -, then look ahead to find one or more letters (but they aren’t part of the “match”
    • REPLACE = _
      • the whole match is replaced by the _; since the only character in the match was the -, it just replaces the - with the _
    • mode = regular expression

    Either of those work for me for your sample data.


    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.

  • @PeterJones
    Thanks for your info.

  • Hello, @venus642, @peterjones, @alan-kilborn and All,

    Two hypotheses must be verified, before executing the regex S/R, below :

    • You must use the N++ version 7.9.1 or a later version, which correctly handles the behavior of the \A assertion

    • You systematically must move the caret to the very beginning of current file ( Ctrl + Home ). This hypothesis is implicit for a Find All in Current Document, a Find in All Opened Documents or a Find All operation !

    So, let’s start from this text :

        test-file\tmp$test-old_name -Value $abc\no-wow

    If we use the following regex S/R :

    SEARCH (?-s)(\$|(?!\A)\G).*?\K(?<=\w)-


    we get the expected text :

        test-file\tmp$test_old_name -Value $abc\no_wow

    Note that I slightly changed the beginning of your second line ( test-file instead of testfile )

    And, as you can see in the output text, that dash character - have not been changed because it is located before the $ symbol !

    On the other hand, the dash char, in the string -Value has not been modified, either, as not following a letter !

    Best regards,


    P.S. :

    Alan, doesn’t this type of regex remind you of something ? Yes, my last generic regex ! Refer to the end part of this post :

    In our present example :

    • BR = \$

    • ER is implicit. Indeed, the syntax (?-s).*? is identical to the regex [^\r\n\f]*?, which represents the virtual Excluded Regex !

    • SR = -

    • RR = _

    An other example in this post     ( where I forgot the (?!\A) look-around ! )

    • BR = \$

    • ER is implicit : again, the Excluded Regex (ER) is the regex [^\r\n]

    • SR = (0)|(1)|(2)

    • RR = (?1G)(?2H)(?3I)

  • @guy038 , @Venus642 ,

    My second regex will work, if you use Replace All. Sorry I forgot to mention it. (\K often requires Replace All)

    My first regex works whether you use multiple replace clicks, or replace all, because it’s using simple groups rather than match-reset or lookbehind.

  • @guy038 said in RegEX - Find and rename special words (letter):


    Yes! This work fine for me…
    Big thanks!

    Thank’s PeterJones and guy038!

  • @Venus642 said in RegEX - Find and rename special words (letter):

    @guy038 said in RegEX - Find and rename special words (letter):


    Yes! This work fine for me…
    Big thanks!

    Thank’s PeterJones and guy038!

  • Hi @venus642,

    Sorry, I updated my search regex to :

    SEARCH (?-s)(\$|(?!\A)\G).*?\K(?<=\w)-

    Refer to my previous post that I updated too, for the hypotheses to respect and some other pieces of information !



Log in to reply