cole cole - 3 months ago 18
R Question

Forcing r to accept "\" into a string to pass as a regular expression to an API [r]

I am trying to figure out how to pass

sessions::condition::ga:pagePath=~search\.html|/content/dam/downloaded_assets/


as a regular expression into an API call through r. The
Error: '\.' is an unrecognized escape in character string starting ""sessions::condition::ga:pagePath=~search\."
happens when I try to pass this string.

I am aware that in r I would need to escape twice
(\\)
for r to accept the string and not throw an error. However, to the API there's a difference between

search\\.html


and

search\.html


where the second example gives me the results I want. Is there any way to force r to not throw an error and pass the string as written above? Or is there another solution?

Thank you

Answer

You could pass it to URLencode first.

stuff <- "sessions::condition::ga:pagePath=~search\\.html|/content/dam/downloaded_assets/"
URLencode(stuff)