[erlang-questions] clarify: escaping strings in erl_scan:string / erl_parse:parse_term

Corrado Santoro <>
Fri Dec 28 14:52:05 CET 2007


You have to quote the backslashes:

7> {ok,Tokens,_}=erl_scan:string("{a_string, \"foo(\\\"hi\\\")\"}.").
{ok,[{'{',1},
      {atom,1,a_string},
      {',',1},
      {string,1,"foo(\"hi\")"},
      {'}',1},
      {dot,1}],
     1}
8> erl_parse:parse_term(Tokens).
{ok,{a_string,"foo(\"hi\")"}}

:-)

Cheers,
--Corrado

Roberto Saccon wrote:
> How do I escape strings  in Erlang before tokenizing / parsing with
> erl_scan:string/1 and erl_parse:parse_term/1 ?
> 
> E.g. in the interactive Erlang shell I simply do:
> 
> 1> {a_string, "foo(\"hi\")"}.
> {a_string,"foo(\"hi\")"}
> 
> 
> But if I try to do the same with erl_scan:string and
> erl_parse:parse_term, it is tokenizing not as expected and parsing
> gives an error:
> 
> 
> 2> {ok,Tokens,_}=erl_scan:string("{a_string, \"foo(\"hi\")\"}.").
> {ok,[{'{',1},
>      {atom,1,a_string},
>      {',',1},
>      {string,1,"foo("},
>      {atom,1,hi},
>      {string,1,")"},
>      {'}',1},
>      {dot,1}],
>     1}
> 3> erl_parse:parse_term(Tokens).
> {error,{1,erl_parse,["syntax error before: ","hi"]}}
> 4>
> 
> 
> So what is the proper way to escape strings in this context ?
> 


-- 
==================================================================
Eng. Corrado Santoro, Ph.D.
University of Catania - ITALY - Engineering Faculty

Tel: +39 095 7382380        VoIP: sip:

Personal Home Page: http://www.diit.unict.it/users/csanto
     NUXI Home Page: http://nuxi.diit.unict.it
==================================================================




More information about the erlang-questions mailing list