[R] How do I parse embedded quotes?

Duncan Murdoch murdoch.duncan at gmail.com
Tue Jan 26 18:29:27 CET 2016


On 26/01/2016 10:44 AM, Andre Mikulec wrote:
> I want embedded quotes to be parsed.  I can not figure out hot to do that.   I want to this to be parsable.
>
> "'"a"'"
>
> In a simpler case, "'a'" is parsable.
>
> > eval(parse(text="\"'a'\""))
> [1] "'a'"
>
>
> But I want to parse embedded quotes
> So, I want to parse '"a"' and not just 'a'
>
> > eval(parse(text="\"'\"a\"'\""))
> Error in parse(text = "\"'\"a\"'\"") : <text>:1:4: unexpected symbol
> 1: "'"a
>
>
> I keep getting that(above) error.
> Any ideas would much be appreciated.

It is usually helpful to print strings using cat(), so that the escapes 
and quotes around it don't show.   With your string that gives the 
error, I see

"'"a"'"

That's not legal R source, because the double quotes that are within the 
string are not escaped.  To be legal R  code you would need

"'\"a\"'"

and to get that you need the original string to look like

"\"'\\\"a\\\"'\""

Duncan Murdoch



More information about the R-help mailing list