• Login
Community
  • Login

Search and Replace with Regex

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 5 Posters 27.3k 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.
  • J
    jens ubert
    last edited by Aug 3, 2019, 4:26 PM

    Hi, I’m trying to clean up some very old code and try to use some RegEx in Notepad++.

    I have a mix of variables:

    A: $somearr_name[some_key]
    B: $somearr_name[‘some_key’]
    C: $somearr_name[$some_key]
    D: ‘$somearr_name[some_key]’

    the goal is to have only:
    $somearr_name[‘some_key’] after substitution.

    somearr_name and some_key should stay untouched

    $somearr_name[$some_key] © and ‘$somearr_name[some_key]’ (D) should not be touched at all.

    1 Reply Last reply Reply Quote 0
    • J
      jens ubert
      last edited by Aug 3, 2019, 6:03 PM

      My Expression to find the matches is:

      [^‘]$(.*?)[^$][^’]

      … but I have no idea, how to handle the replacement …

      E A 2 Replies Last reply Aug 3, 2019, 8:59 PM Reply Quote 1
      • E
        Ekopalypse @jens ubert
        last edited by Aug 3, 2019, 8:59 PM

        @jens-ubert

        not really sure I get what you try to achieve but what about
        find what:(?<!')(\$[\w]+\[)([\w]+)(?=\])
        replace with:\1'\2'

        1 Reply Last reply Reply Quote 3
        • A
          Alan Kilborn @jens ubert
          last edited by Aug 3, 2019, 9:09 PM

          @jens-ubert

          Maybe try:

          Bring up the Replace window (ctrl+h).
          Find what box: (\$\w+)\[(\w+)\](?!')
          Replace with box: \1['\2']
          Search mode: Regular expression
          Wrap around: ticked
          Press the Replace All button.

          1 Reply Last reply Reply Quote 2
          • J
            jens ubert
            last edited by Aug 4, 2019, 8:23 AM

            ohhhh … it looks pretty! I’ll try little more und will give some response.
            Did not know about \1 … because of my poor english and knowledge about the terminology in this case: How is this kind of replacement called.

            A 1 Reply Last reply Aug 4, 2019, 11:51 AM Reply Quote 0
            • A
              Alan Kilborn @jens ubert
              last edited by Aug 4, 2019, 11:51 AM

              @jens-ubert said:

              How is this kind of replacement called

              Hmmm, not sure exactly…maybe replacement by captured group number ? Go look it up and see if it has a better name.

              1 Reply Last reply Reply Quote 1
              • P
                PeterJones
                last edited by Aug 4, 2019, 10:29 PM

                @jens-ubert said:

                How is this kind of replacement called.

                The Boost documentation calls it a “backreference ”. In the replacement string, the $-based equivalent is called a “placeholder sequence ”.

                1 Reply Last reply Reply Quote 3
                • G
                  guy038
                  last edited by guy038 Aug 4, 2019, 11:56 PM Aug 4, 2019, 11:43 PM

                  Hello, @jens-ubert, @ekopalypse, @alan-kilborn, @peterjones, and All,

                  Jens, welcome to the N++ community !

                  Seemingly, I understand that your goal is to obtain the B) syntax, leaving the C) and D syntaxes untouched. In other words, you just would like that any A) syntax be replaced with the B syntax, wouldn’t you ?

                  Thus, we have to grab any A syntax, specifically, and change it into the B one

                  Comparing all syntaxes, it comes that only the A syntax does not contain any single quote character, ' nor the $ symbol, between square brackets. We can add the rule that the variable name, before the square brackets does not contain any single quote, too !

                  On the other hand, the part before square brackets does not change, when moving from the A to the B syntax. So, we just need to store the text, between square brackets, and surround it with single quotes, during replacement !


                  So, a possible regex would be :

                  SEARCH \s\$[^'\r\n]+\[\K([^'$\r\n]+)(?=\])

                  REPLACE '\1'

                  Notes :

                  • The beginning of the regex \s searches for the general kind of space character ( the Space or Tabulation chars, the \n or \r line break chars and some others… )

                  • Then the part \$ looks for the literal regex symbol $

                  • Now, the syntax [^'\r\n]+\[ searches the greatest non-null range of consecutive characters, different from a single quote and line-breaks chars, followed by a literal opening square bracket

                  • The special syntax \K cancels any match and resets the regex engine working position

                  • Thus, the part ([^'$\r\n]+) tries, now, to match the greatest non-null range of consecutive characters, either different from a single quote, a dollar and line-breaks chars, stored as group 1, due to the outer parentheses

                  • But ONLY IF  the look-ahead (?=\]) is true, i.e. if this range is followed with an ending literal square bracket !

                  • Finally, in replacement, the \1 syntax represents all group 1 contents, surrounded with single quotes

                  Best Regards,

                  guy038

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