How to replace or remove the \ character?

Hi @Thoralf , you are right that the forum software gives special meaning to some characters, and if pasting certain text (especially if you want it to appear exactly as written) you need to highlight it and use the “preformatted text” button on the forum toolbar:
image.

This also prevents it doing unwanted conversions of "double quotes" into “smart quotes”.

In the same way, regex, along with some other scripting languages, gives special meaning to the \ character so if you want it to be treated as an actual \ and not say as an “escape” character, it is common to have to double-up so you are “escaping” it to give it its literal meaning.

If you have some text:
"firstname \"fifa\" familyname"

and you want to use String Manipulation to remove all substrings "…", you could use the following:

regexReplace($column1$,"(\\\\\".*?\\\\\")","")

I’ll try to explain the presence of two sets of 5 backslashes… :wink:

In order that String Manipulation does not treat the enclosed " characters as string terminators, these need to be escaped by placing a \ in front of each.

Additionally, string manipulation treats \ characters as “escape” characters (such as we just used to escape the double-quote, so in order that it not treat these as “escape characters” they themselves need to be escaped, so each \ needs to become two \ and this allows the final pattern that arrives at regex to be:

(\\".*?\\")

which is what we actually want. This is then ok for regex because it also needs to handle escape charaters, and those escaped backslashes are then treated as a single literal \ by regex.

It can be “fun” but here is my basic rule of thumb:

Write the regex pattern that you think you will require, and then add an extra \ in front of each \ in the regex, and add an additional \ in front of every " (double-quote) contained within the regex pattern (but don’t try to escape the actual string terminators required by String Manipulation!). You should generally then end up with what you need for String Manipulation.


tip: When you run the above code, you may find that you have double-spaces in the middle of your resultant string. If this is undesirable, you could wrap the whole regexReplace(...) function with a call to removeDuplicates(...) which will remove all duplicate spaces.
e.g.

5 Likes