# How do I make a string literal without having to escape backslashes?

I'd like to say something like:

foo = @"\abc\def\ghi"


foo = "\\abc\\def\\ghi"


How do I do this? This would be really nice for regular expressions.

• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey Mar 6 '15 at 21:38

We can implement a raw string syntax using the global variable $PreRead, which "is applied to the text or box form of every input expression before it is fed to the Wolfram Language." Using $PreRead, we can get at the string before any interpretation has been performed on it, replace it with the corresponding InputForm expression, and send that along as input to Mathematica.

Here we define a RawString "head" that signals that the RawString expression will be automatically replaced by the InputForm of its argument before being used as input:

\$PreRead = (
# /. RowBox@{"RawString", "[", str_String, "]"} |
RowBox@{"RawString", "@", str_String} |
RowBox@{str_String, "//", "RawString"} :>
RowBox@{"StringTrim", "[", ToString@InputForm@str, ",", "\"\\\"\"", "]"}
)&;


Note that we are dealing with the box form prior to any evaluation, so we have to use separate patterns for prefix and postfix notation. The StringTrim is inserted so that it will take effect after the input string is interpreted to remove the extraneous opening and closing quotes.

Now we can enter

"\abc\def\ghi\nop" // RawString


or

RawString@"\abc\def\ghi\nop"


and we get

\abc\def\ghi\nop


The RawString expressions can be part of other expressions:

RawString@"\pi\ " <> RawString@"\nu\ "
(* \pi\ \nu\ *)


as long as each raw string is explicitly the argument of a RawString, i.e.,

RawString /@ {"\pi\ ", "\nu\ "}


doesn't work.

To enter the string in without double backslash, you could use a TextCell as follows:

ToString@@Ctrl-9\abc\def\ghiCtrl-0

\abc\def\ghi

FullForm[%]


"\\abc\\def\\ghi"

On the first line, I open a text cell and enter the text as you wanted to type it. The ToString knows how to convert the text cell to a string, as is shown in the last FullForm output.

You could also replace ToString@@ by First@.

It is not a direct input form as you describe but Mathematica will insert the escapes for you in the right circumstance. If you copy the verbatim text \abc\def\ghi\nop and paste it between quotation marks in a Notebook you will get a dialog:

Click Yes and you get:

"\\abc\\def\\ghi\\nop"


If the string is in an external file Import will handle it correctly. For example if we put \abc\def\ghi\nop in a file named foo.txt we can then use:

Import["foo.txt", "String"] // FullForm

"\\abc\\def\\ghi\\nop"


The FullForm is added merely to show that the escaped \\ are in fact present.