How print escape chars from variable using IO.puts?

Hello, i trying to print something like:

iex> expression = "^\d\d\d\d"
iex> IO.puts expression
"^^^^^"

but i looking for output “^\d\d\d\d”

thanks

Use a “raw” string, ~S"^\d" or escape the backslashes "^\\d".

thanks, there is another way?

i generate a complex xml who have regular expressions literals, like:

<condition pattern="123\d\d\d"/>

when i write to file File.write or print with IO.puts the regular expression are missing i see the same problem with Plug.send_resp

As I said, write double backslashes in your code or use uppercase s sigil.

If though you do not have this in your code but read them from somewhere else, then it’s another issue that you need to explain better.

2 Likes