问题
I have a string Project Change Request (PCR) - HONDA DIGITAL PLATEFORM
saved in supp_matches
, and supp_matches1
contains the string Project Change Request (PCR) -
.
supp_matches2 <- gsub("^.*[supp_matches1]","",supp_matches)
supp_matches2
# [1] " (PCR) - HONDA DIGITAL PLATEFORM"
Which is actually not correct but it should come like
supp_matches2
# [1] "HONDA DIGITAL PLATEFORM"
Why is it not coming the way it should be?
回答1:
As I say in my comment, in your expression gsub("^.*[supp_matches1]", "", supp_matches)
, you're not really using the object supp_matches1
but just the letters inside it.
You could do something like gsub(paste0("^.*", supp_matches1), "", supp_matches)
to really use the expression contained in supp_matches1
, except that, as mentionned by @rawr, you have parentheses in your expression so you would need to excape them.
The correct expression to get what you want would then be sub("Project Change Request \\(PCR\\) - ", "", supp_matches)
To get what you want, you can use the fixed
parameter of gsub
(sub
) function, which is saying that the expression in the parameter pattern
will be matched as it is (so, without the need to escape anything, but also, no real regular expression).
So what's you are looking for is :
gsub(supp_matches1, "", supp_matches, fixed=TRUE) # or just with `sub` in this case
#[1] "HONDA DIGITAL PLATEFORM"
回答2:
Already @cathG provided an answer with fixed=TRUE. If you want to do all with regex, then you may try this.
> w1 <- "Project Change Request (PCR) - HONDA DIGITAL PLATEFORM"
> w2 <- "Project Change Request (PCR) - "
> sub(paste0("^", gsub("(\\W)", "\\\\\\1", w2)), "", w1)
[1] "HONDA DIGITAL PLATEFORM"
It's just a kind of escaping all the special chars present inside the variable you want to use as first parameter in sub function.
来源:https://stackoverflow.com/questions/31267208/removing-a-pattern-with-gsub-in-r