replace \$ with $ in string

346 views
Skip to first unread message

will ship

unread,
Apr 26, 2015, 8:27:09 AM4/26/15
to julia...@googlegroups.com
Hi

I am reading in a text file with special characters e.g. : " Blah is $LBP"
when I read it in to a variable the "$" gets replaced with "\$". But I actually want the $ non-escaped so that when handling the string variable it prints the $LBP value.

Is there anyway of doing this, our just to remove the "\" in the string without removing the "$" as well?

Thanks in advance.

Will

Tamas Papp

unread,
Apr 26, 2015, 8:44:05 AM4/26/15
to julia...@googlegroups.com
Maybe something like

julia> s = "my \\ escaped \\\$ \$ string"
"my \\ escaped \\\$ \$ string"

julia> replace(s, "\\\$", "\$")
"my \\ escaped \$ \$ string"

Best,

Tamas

PS.: Incidentally, is there a way to enter strings with $ without
escaping them? Ie something like a non-standard string literal that just
ignores the $'s.

Sean Marshallsay

unread,
Apr 26, 2015, 8:57:21 AM4/26/15
to julia...@googlegroups.com
Tamas, you can define a macro `macro raw_str(s) s end` which does what I think you want:

julia> macro raw_str(s) s end

julia> raw"This macro \t escapes \n any $special $(characters) for you."
"This macro \\t escapes \\n any \$special \$(characters) for you."
Message has been deleted

Tamas Papp

unread,
Apr 26, 2015, 9:10:01 AM4/26/15
to julia...@googlegroups.com
Thanks --- this is almost what I want, except that I would like to
escape everything _but_ the $'s, ie have a string literal like C and
ilk. I think that

macro semiraw_str(s)
parse(string("\"",replace(s, "\$", "\\\$"),"\""))
end

does this but it looks like something that should be taken out and
shot -- de-uglification suggestions are gladly accepted.

Best,

Tamas

On Sun, Apr 26 2015, Sean Marshallsay <srm....@gmail.com> wrote:

> Tamas, you can define a macro `macro raw_str(s) s end` which does what I
> think you want:
>
> julia> macro raw_str(s) s end
>
> julia> raw"This macro \t escapes \n any $special $(characters) for you."
> "This macro \\t escapes \\n any \$special \$(characters) for you."
>
>
>
> On Sunday, 26 April 2015 13:44:05 UTC+1, Tamas Papp wrote:
>>
>> Maybe something like
>>
>> julia> s = "my \\ escaped \\\$ \$ string"
>> "my \\ escaped \\\$ \$ string"
>>
>> julia> replace(s, "\\\$", "\$")
>> "my \\ escaped \$ \$ string"
>>
>> Best,
>>
>> Tamas
>>
>> PS.: Incidentally, is there a way to enter strings with $ without
>> escaping them? Ie something like a non-standard string literal that just
>> ignores the $'s.
>>
>> On Sun, Apr 26 2015, will ship <willshi...@gmail.com <javascript:>>

will ship

unread,
Apr 26, 2015, 9:11:27 AM4/26/15
to julia...@googlegroups.com
Hi Tamas I actually want to completely remove the "\" before the "$"  as I want the following type of operation:

julia> LBP=30
30

julia> string ="LBP =$LBP"
"LBP =30"

Cheers 
Will

Tom Lee

unread,
Apr 26, 2015, 12:47:51 PM4/26/15
to julia...@googlegroups.com
My understanding, based on the description in the manual, is that what you are trying to do won't be so simple. It sounds like string interpolation works by having the compiler replace
"LBP = $LBP"
with
string("LBP = ",LBP)

or something like this - hopefully someone will correct me if I am mistaken. There is probably a way you could get around this limitation with metaprogramming, though depending on what you are trying to achieve overall, it might be simpler to do some kind of find and replace on your string using values stored in a dictionary.

Cheers,

Tom

Jameson Nash

unread,
Apr 26, 2015, 2:16:42 PM4/26/15
to julia...@googlegroups.com
I think the replies here may have missed your statement that you are "reading a text file" not "writing these strings in you code" (or perhaps I misread something).

When Julia displays a string in the REPL, it adds escape characters and quotes so that you can copy it back into other Julia context and get back the same string. But these characters are not actually part of the string, any more than the " that is used to indicate that the type of the object is a string:

julia> x="asdf\$LPB"
"asdf\$LPB"

julia> display(x)
"asdf\$LPB"

julia> println(x)
asdf$LPB

Tom Lee

unread,
Apr 26, 2015, 9:36:02 PM4/26/15
to julia...@googlegroups.com
This seems to do what you need, but I am not sure it is really the best approach. It will probably be quite slow, and may allow hackers to break into your house and steal your children... Use at own risk!

function interpolate(str)
       str
= string("\"", str, "\"")
       
eval(parse(str))
end


julia> LBP=30 ; s="LBP = \$LBP" ; interpolate(s)
"LBP = 30"


Cheers,

Tom

On Sunday, 26 April 2015 09:11:27 UTC-4, will ship wrote:
Reply all
Reply to author
Forward
0 new messages