Community
    • Login

    regex - search and replace - "remember"?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 4 Posters 2.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.
    • Adam YikA
      Adam Yik
      last edited by Adam Yik

      I don’t know where in the document mentions where you can take what ever it has been found, and copies text and is pasted in the “replace with”. For example:

      abcd/1234/efgh
      ijkl/5678/mnop
      

      becomes:

      abcd/efgh/1234
      ijkl/mnop/5678
      

      (it’s swapped, by being replaced with [abcd/<memory 2>/<memory 1>]).

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Adam Yik
        last edited by

        @Adam-Yik

        Open the Replace dialog by pressing Ctrl+h and then set up the following search parameters:

        Find what box: (?-s)(.{4})/(.{4})$
        Replace with box: \2/\1
        Search mode radiobutton: Regular expression
        Wrap around checkbox: ticked
        . matches newline checkbox: doesn’t matter (because the (?-s) leading off the Find what box contains an s variant)

        Then press the Replace All button

        Adam YikA 1 Reply Last reply Reply Quote 2
        • EkopalypseE
          Ekopalypse
          last edited by

          Hmm … question is quite similar to this one, strange.

          1 Reply Last reply Reply Quote 0
          • Adam YikA
            Adam Yik @Alan Kilborn
            last edited by

            @Alan-Kilborn So, it’s something to do with a backreference. Is this caused by the parenthesis? Because the . means to match any character and {ℕ} is the number of characters. Also, do backreferences’s numbering:

            -Starts at 0?
            -Is numbered from left to right (example, the first parenthesis group is numbered 0 and the last is numbered 2)?

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Adam Yik
              last edited by

              @Adam-Yik said in regex - search and replace - "remember"?:

              Is this caused by the parenthesis?

              Yes.

              do backreferences’s numbering: Starts at 0?

              No, they start at 1, and they aren’t called backreferences, they’re called “capture groups”.

              A pseudo-group #0 is for the overall match and is accessed at replace time as $0 rather than \0.

              Is numbered from left to right

              Yes.

              he first parenthesis group is numbered 0 and the last is numbered 2)?

              No, but close. The first is numbered 1 and the second/last is 2.

              Adam YikA 2 Replies Last reply Reply Quote 2
              • Adam YikA
                Adam Yik @Alan Kilborn
                last edited by

                @Alan-Kilborn thanks! sadly, up to 9 can be remembered within parent group at a time. But I think you can get around this by doing this multiple times.

                1 Reply Last reply Reply Quote -1
                • Adam YikA
                  Adam Yik @Alan Kilborn
                  last edited by

                  @Alan-Kilborn To fully understand the subgroup, it is very similar to Directory-based system such as windows 10. However, up to 9 files and folders can exist in a directory, and to refer them on a path, you place each digit and without the path separator /

                  Alan KilbornA 1 Reply Last reply Reply Quote -1
                  • PeterJonesP
                    PeterJones
                    last edited by

                    @Adam-Yik said in regex - search and replace - "remember"?:

                    thanks! sadly, up to 9 can be remembered within parent group at a time

                    Actually, there is no such limitation. The substitutions section of the official docs says,

                    $ℕ, ${ℕ}, \ℕ ⇒ Returns what matched the ℕth subexpression, where ℕ is a positive integer (1 or larger).

                    By using the ${ℕ} notation, you can reference any capture group, not just the first 9. So the fifteenth group would be ${15} in the replacement expression.

                    1 Reply Last reply Reply Quote 1
                    • Alan KilbornA
                      Alan Kilborn @Adam Yik
                      last edited by

                      @Adam-Yik said in regex - search and replace - "remember"?:

                      it is very similar to Directory-based system such as windows 10. However, up to 9 files and folders can exist in a directory, and

                      There is no such limitation in Windows, and it doesn’t have anything to do with regular expression processing. Please don’t spread misinformation.

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