Community
    • Login

    RegEX - Find and rename special words (letter)

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    8 Posts 3 Posters 3.5k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Venus642V
      Venus642
      last edited by

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

      All replace words beginn with $:

      Example:

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

      My wish:
      $this_is_my_test
      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?
      Thanks!

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Venus642
        last edited by

        @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.

        Venus642V 1 Reply Last reply Reply Quote 2
        • Venus642V
          Venus642 @PeterJones
          last edited by

          @PeterJones
          Thanks for your info.

          1 Reply Last reply Reply Quote 0
          • guy038G
            guy038
            last edited by guy038

            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 :

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

            If we use the following regex S/R :

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

            REPLACE _

            we get the expected text :

                $this_is_my_test
            
                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,

            guy038

            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 :

            https://community.notepad-plus-plus.org/post/62131

            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    https://community.notepad-plus-plus.org/post/62396     ( 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)

            PeterJonesP Venus642V 2 Replies Last reply Reply Quote 1
            • PeterJonesP
              PeterJones @guy038
              last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • Venus642V
                Venus642 @guy038
                last edited by

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

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

                Yes! This work fine for me…
                Big thanks!

                Thank’s PeterJones and guy038!

                Venus642V 1 Reply Last reply Reply Quote 1
                • Venus642V
                  Venus642 @Venus642
                  last edited by

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

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

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

                  Yes! This work fine for me…
                  Big thanks!

                  Thank’s PeterJones and guy038!

                  1 Reply Last reply Reply Quote 0
                  • guy038G
                    guy038
                    last edited by 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 !

                    BR

                    guy038

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors