Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

gsub not working for string with regex character (*)

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading