0

I am trying to execute the below code

library(purrr)
library(glue)
library(tidyquant)

gsub("https://abc.a/:x:/s/asd/esdfgy?=jo", " % s ", "This is the link https://abc.a/:x:/s/asd/esdfgy=jo")
[1] "This is the link  % s "

Above execution is correct. But when I include ? in my text the output is different. Can i have a same output like above?

gsub("https://abc.a/:x:/s/asd/esdfgy?=jo", " % s ", "This is the link https://abc.a/:x:/s/asd/esdfgy?=jo")
[1] "This is the link https://abc.a/:x:/s/asd/esdfgy?=jo"

Expected output

[1] "This is the link  % s "
manu p
  • 952
  • 4
  • 10
  • 1
    `?` has a special meaning in regex. Use `\\?`. `gsub("https://abc.a/:x:/s/asd/esdfgy\\?=jo", " % s ", "This is the link https://abc.a/:x:/s/asd/esdfgy?=jo")` – Ronak Shah Sep 17 '21 at 08:22
  • But I am trying to replace ? with //? with this code ````str_replace("sdsdsz?","?","\\?")```` but not happening? – manu p Sep 17 '21 at 08:33
  • why do you want to replace `?` with `?` and you need to escape the pattern and not replacement. Try `str_replace("sdsdsz?","\\?","!")` – Ronak Shah Sep 17 '21 at 08:35
  • as you mentioned use ````\\?````. So when I have ? , I will replace with \\?. But your suggestion is not working ````str_replace("sdsdsz?","\\?","!")```` – manu p Sep 17 '21 at 08:39
  • Hope you got my point :) – manu p Sep 17 '21 at 08:48
  • So basically ````This is the link https://abc.a/:x:/s/asd/esdfgy?=jo```` is the link, We cannot escape ? here and it will be a invalid url. So only – manu p Sep 17 '21 at 08:53

0 Answers0