Community
    • Login

    Replace all extended search tokens (\n,\t,...) with corresponding patterns

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 7.6k 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.
    • daMatzDaMatzD
      daMatzDaMatz
      last edited by

      Hi!

      Is it possible to replace all \n, \t, … with newlines, tabs etc?
      Example:
      If I have a text that looks like this:

      This\nis\na nice\ttext
      

      I would like to transform it to

      This
      is
      a nice    text
      

      and the other way around.
      Currently I replace \n with a placeholder string (e.g. **********) and then the other way around with “Extended” option activated, but this needs to be done for every token :(

      Alan KilbornA 1 Reply Last reply Reply Quote 2
      • Alan KilbornA
        Alan Kilborn @daMatzDaMatz
        last edited by Alan Kilborn

        @daMatzDaMatz

        Find: \\n
        Replace: \n
        Search mode: Regular expression

        But…are you sure you want to end up with non-Windows line endings?
        You may really want:
        Replace: \r\n

        Tabs would be similar:

        Find: \\t
        Replace: \t
        Search mode: Regular expression

        daMatzDaMatzD 1 Reply Last reply Reply Quote 2
        • daMatzDaMatzD
          daMatzDaMatz @Alan Kilborn
          last edited by

          @Alan-Kilborn

          Thank you! This already helps to reduce the steps in half!
          It would be awesome if it is possible to replace all tokens at once (\\n with \n, \\t with \t, \\r with \r, etc.). Do you also have a trick for that? I often have to deal with stacktraces that are otherwise unreadable and a one click solution would be great :)

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

            @daMatzDaMatz ,

            if it is possible to replace all tokens at once

            Use capture groups and conditional replacement:

            • FIND = (\\n)|(\\r)|(\\t)
            • REPLACE = (?1\n)(?2\r)(?3\t)

            You can record this as a macro for even faster access

            Explanation:

            • \\ = literal backslash in your existing text
            • | = alternate option
            • () = put anything matched inside these parentheses into the next capture-group number
            • replacement (?1\n) = if capture-group #1 was found, put \n (carriage return) in the replacement, otherwise put nothing

            since there are three conditionals, one for each potential match, if it matches any of the three literal texts, it will replace it with the special character.

            The same formula can be easily expanded. Though if you do more than 9 capture-groups in the FIND, you will need to use {} around the conditional number, like (?{13}Thirteen) to put the literal word Thirteen in the replacement if the thirteenth group had a match.

            ----

            Useful References

            • Notepad++ Online User Manual: Searching/Regex
            • FAQ: Where to find regular expressions (regex) documentation
            daMatzDaMatzD 1 Reply Last reply Reply Quote 2
            • daMatzDaMatzD
              daMatzDaMatz @PeterJones
              last edited by

              @PeterJones

              This is awesome and exactly what I was looking for, thanks for taking the time and explaining everything!
              Just recorded the macro and will use it in the feature!

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