I'd like to say something like:

foo = @"\abc\def\ghi" 

instead of

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

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

  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Mar 6, 2015 at 21:38
  • $\begingroup$ I think that answers to this question should address cases when one is working directly in the Kernel without a front-end or with another front-end like Jupyter notebook and Wolfram Engine. $\endgroup$
    – rhermans
    May 5, 2022 at 14:26

3 Answers 3


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



and we get


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:





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:

enter image description here

Click Yes and you get:


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

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

  • $\begingroup$ What if one is working directly in the Kernel, and no front-end or with another front-end like Jupyter notebook and Wolfram Engine? $\endgroup$
    – rhermans
    May 5, 2022 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.