• Login
Community
  • Login

regex - search and replace - "remember"?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 4 Posters 2.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.
  • A
    Adam Yik
    last edited by Adam Yik Apr 7, 2020, 9:58 PM Apr 7, 2020, 9:57 PM

    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>]).

    A 1 Reply Last reply Apr 7, 2020, 10:09 PM Reply Quote 0
    • A
      Alan Kilborn @Adam Yik
      last edited by Apr 7, 2020, 10:09 PM

      @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

      A 1 Reply Last reply Apr 7, 2020, 10:49 PM Reply Quote 2
      • E
        Ekopalypse
        last edited by Apr 7, 2020, 10:14 PM

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

        1 Reply Last reply Reply Quote 0
        • A
          Adam Yik @Alan Kilborn
          last edited by Apr 7, 2020, 10:49 PM

          @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)?

          A 1 Reply Last reply Apr 7, 2020, 11:09 PM Reply Quote 0
          • A
            Alan Kilborn @Adam Yik
            last edited by Apr 7, 2020, 11:09 PM

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

            A 2 Replies Last reply Apr 7, 2020, 11:22 PM Reply Quote 2
            • A
              Adam Yik @Alan Kilborn
              last edited by Apr 7, 2020, 11:22 PM

              @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
              • A
                Adam Yik @Alan Kilborn
                last edited by Apr 7, 2020, 11:38 PM

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

                A 1 Reply Last reply Apr 8, 2020, 12:16 AM Reply Quote -1
                • P
                  PeterJones
                  last edited by Apr 7, 2020, 11:44 PM

                  @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
                  • A
                    Alan Kilborn @Adam Yik
                    last edited by Apr 8, 2020, 12:16 AM

                    @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
                    3 out of 9
                    • First post
                      3/9
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors