• Login
Community
  • Login

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

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 3 Posters 7.8k 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.
  • D
    daMatzDaMatz
    last edited by Oct 20, 2022, 2:09 PM

    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 :(

    A 1 Reply Last reply Oct 20, 2022, 2:27 PM Reply Quote 2
    • A
      Alan Kilborn @daMatzDaMatz
      last edited by Alan Kilborn Oct 20, 2022, 2:27 PM Oct 20, 2022, 2:27 PM

      @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

      D 1 Reply Last reply Oct 20, 2022, 2:51 PM Reply Quote 2
      • D
        daMatzDaMatz @Alan Kilborn
        last edited by Oct 20, 2022, 2:51 PM

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

        P 1 Reply Last reply Oct 20, 2022, 2:59 PM Reply Quote 0
        • P
          PeterJones @daMatzDaMatz
          last edited by PeterJones Oct 20, 2022, 3:01 PM Oct 20, 2022, 2:59 PM

          @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
          D 1 Reply Last reply Oct 20, 2022, 3:03 PM Reply Quote 2
          • D
            daMatzDaMatz @PeterJones
            last edited by Oct 20, 2022, 3:03 PM

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