Home > Net >  gsub not working for string with regex character (*)
gsub not working for string with regex character (*)

Time:10-28

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

CodePudding user response:

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)

NOTE: If you just want to get rid of the - Copia text, then use Wasim Aftab's answer which is much more straightforward.

  •  Tags:  
  • r
  • Related