0

Hi and thanks in advance for any help

I have a long text ,it contains a lot of info but I am looking to substract a part of it.

the structure of the text and what I need is like this

" requires_shipping = c(TRUE, TRUE), taxable = c(TRUE, TRUE), gift_card = c(FALSE, FALSE), tax_code = c(\"PC040204\", \"PC040204\"\n), name = c(\"Julian Backpack\", \"Mini M.A.C. Crossbody\"), variant_inventory_management = c(\"shopify\", \"shopify\") "

what I am looking for is the items inside the "name" separated by comas so in this case the result would be

"\"Julian Backpack\", \"Mini M.A.C. Crossbody\"

I am using the stringr package and trying to use the str_subset or grep

when I use test my regular expression on https://regex101.com/

I use this regex \bname\s[=].+?(?=\)) and I am able to get this match

name = c(\"Julian Backpack\", \"Mini M.A.C. Crossbody\"

but when I try to use it in R I get an error

grep("\bname\s[=].+?(?=\))", string)
Error: '\s' is an unrecognized escape in character string starting ""\bname\s"

Thanks again

jlozasi
  • 57
  • 4
  • R reads `\s` in a string as a special escape character and tries to interpret it. Since you want to include an actual `\s` in that string to pass to the regex, you need to escape the backslash: `\\s` – divibisan May 24 '19 at 22:01
  • Possible duplicate of [Error: '\R' is an unrecognized escape in character string starting "C:\R"](https://stackoverflow.com/questions/12695879/error-r-is-an-unrecognized-escape-in-character-string-starting-c-r) – divibisan May 24 '19 at 22:01

1 Answers1

0

Using sub

> sub(".*name.*\\((.*)\\),.*$", "\\1", string)
[1] "\"Julian Backpack\", \"Mini M.A.C. Crossbody\""
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138