# How to convert a raw string to escape $

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-raw-string-to-escape/112141>\
**Category:** New to Julia\
**Created:** [March 26, 2024, 1:02pm UTC](https://discourse.julialang.org/t/how-to-convert-a-raw-string-to-escape/112141 "2024-03-26T13:02:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simone\_Gabbriellini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simone_gabbriellini/32/202218_2.png) [@Simone\_Gabbriellini](https://discourse.julialang.org/u/Simone_Gabbriellini)\
**Post date:** [March 26, 2024, 1:02pm UTC](https://discourse.julialang.org/t/how-to-convert-a-raw-string-to-escape/112141/1 "2024-03-26T13:02:55Z")

</div>

I have a situation where I am making a call to OpenAI and I get back a string, which might contain a `$` like in “the price of the ticket is 10$”.

My problem is how to articulate the escape of the `$` sign:

```julia
raw_string = r"the price of the ticket is 10$"
normal_string = replace(raw_string, r"$" => "\$")

```

is not working because it throws this error:

```julia
MethodError: no method matching similar(::Regex, ::Type{Any})

Closest candidates are:
  similar(!Matched::VSCodeServer.JuliaInterpreter.Compiled, ::Any)
   @ VSCodeServer ~/.vscode-server/extensions/julialang.language-julia-1.73.2/scripts/packages/JuliaInterpreter/src/types.jl:7
  similar(!Matched::LinearAlgebra.UpperHessenberg, ::Type{T}, !Matched::Tuple{Vararg{Int64, N}}) where {T, N}
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/hessenberg.jl:64
  similar(!Matched::LinearAlgebra.UpperHessenberg, ::Type{T}) where T
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/hessenberg.jl:63
  ...

Stacktrace:
 [1] _similar_or_copy(x::Regex, ::Type{Any})
   @ Base ./set.jl:572
 [2] replace(A::Regex, old_new::Pair{Regex, String}; count::Nothing)
   @ Base ./set.jl:690
 [3] replace(A::Regex, old_new::Pair{Regex, String})
   @ Base ./set.jl:686
 [4] top-level scope
   @ /mnt/data/projects/GenieGiano/test.ipynb:4

```

tbh I am kind of lost, I have tried many escape variations with more than one \, but nothing works… any help more than welcome!

---

<div class="post-metadata">

**Author:** ![jbshannon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbshannon/32/38838_2.png) [@jbshannon](https://discourse.julialang.org/u/jbshannon)\
**Post date:** [March 26, 2024, 1:08pm UTC](https://discourse.julialang.org/t/how-to-convert-a-raw-string-to-escape/112141/2 "2024-03-26T13:08:09Z")

</div>

The `raw_string` you create is not actually a raw string, it’s a `Regex`. To create a raw string, prepend the string with `raw` rather than `r`.

```julia-repl
julia> r_string = r"the price of the ticket is 10$"
r"the price of the ticket is 10$"

julia> typeof(r_string)
Regex

julia> raw_string = raw"the price of the ticket is 10$"
"the price of the ticket is 10\$"

julia> println(raw_string)
the price of the ticket is 10$

```

Check the help entries for `@r_str` and `@raw_str` for more info.

---

<div class="post-metadata">

**Author:** ![Simone\_Gabbriellini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simone_gabbriellini/32/202218_2.png) [@Simone\_Gabbriellini](https://discourse.julialang.org/u/Simone_Gabbriellini)\
**Post date:** [March 26, 2024, 1:20pm UTC](https://discourse.julialang.org/t/how-to-convert-a-raw-string-to-escape/112141/3 "2024-03-26T13:20:45Z")

</div>

thank you, it all makes sense now!

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [March 26, 2024, 7:24pm UTC](https://discourse.julialang.org/t/how-to-convert-a-raw-string-to-escape/112141/4 "2024-03-26T19:24:36Z")

</div>

> [@Simone\_Gabbriellini](#):
>
> I have a situation where I am making a call to OpenAI and I get back a string, which might contain a `$` like in “the price of the ticket is 10$”.
> 
> My problem is how to articulate the escape of the `$` sign:

It’s not clear that you’ll have to do this.

```julia
julia> esc = "dollar-escaped \$ string"
"dollar-escaped \$ string"

julia> print(esc)
dollar-escaped $ string
julia> io = IOBuffer();

julia> write(io, esc)
24

julia> String(take!(io))
"dollar-escaped \$ string"

```

In other words, a string you get back from an API call will just have a `$` in it, which is harmless on its own. It will print as `\$` at the REPL, but if you run a replace on it, what you’ll have is:

```julia
julia> replace(esc, raw"$" => raw"\$")
"dollar-escaped \\\$ string"

```

Which might not be what you actually want.
