Replace all extended search tokens (\n,\t,...) with corresponding patterns
-
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 :( -
Find:
\\n
Replace:\n
Search mode: Regular expressionBut…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 -
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 :) -
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 wordThirteen
in the replacement if the thirteenth group had a match.----
Useful References
- FIND =
-
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!