gsub not working for string with regex character (*)

Omar Gonzales

test data:

new <- structure(list(date = structure(c(19289, 19290, 19291), tzone = "America/Bogota", class = "Date"), 
                      tracking_code = c("ppl-rmkt-aaa-aaa-aaa-20221024-pdp-preciopromo-none - Copia_tobuy", 
                                        "ppl-rmkt-aaa-aaa-aaa-20221024-pdp-preciopromo-none - Copia_tobuy", 
                                        "ppl-rmkt-aaa-aaa-aaa-20221024-pdp-preciopromo-none - Copia_tobuy"
                      ), visits = c(81L, 172L, 234L), orders = c(0L, 2L, 0L), units_purchase_event = c(0L, 
                                                                                                       2L, 0L), revenue_purchase_event = c(0, 8698, 0), revenue_dolars_sin_igv = c(0, 
                       

code:

new$tracking_code <- gsub(
  "ppl-rmkt-aaa-aa[*]a-aaa-20221024-pdp-preciopromo-none - Copia$",
  "ppl-lal-aaa-aa*a-aaa-20221024-pdp-preciopromo-none",
  new$tracking_code,
  ignore.case = TRUE
)

Instead of:

ppl-rmkt-aaa-aa*a-aaa-20221024-pdp-preciopromo-none - Copia_tobuy

I'm expecting:

ppl-lal-aaa-aa*a-aaa-20221024-pdp-preciopromo-none_tobuy
shaun_m

There are 2 issues I see in the gsub pattern argument:

  • instead of [*] with \\* to match the literal * character.
  • remove the $ at the end, which is looking for the end of the source string.

So:

new$tracking_code <- gsub("ppl-rmkt-s22ultra128gbgreen-sm\\*s908ezglltp-cyberwow-20221024-pdp-preciopromo-none - Copia",
                              "ppl-lal-s22ultra128gbgreen-sm*s908ezglltp-cyberwow-20221024-pdp-preciopromo-none", new$tracking_code,
                              ignore.case = TRUE)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related