Reputation: 905
This is a silly question, but I can't seem to find a solution in R online. I am trying to remove an isolated number from a long string. For example, I would like to remove the number 27198 from the sentence below.
x <- "hello3 my name 27198 is 5joey"
I tried the following:
gsub("[0-9]","",x)
Which results in:
"hello my name is joey"
But I want:
"hello3 my name is 5joey"
This seems really simple, but I am not well versed with regular expressions. Thanks for your help!
Upvotes: 1
Views: 144
Reputation: 886948
We can specify word boundary (\\b
) at the end of one or more digits ([0-9]+
)
gsub("\\b[0-9]+\\b", "", x)
#[1] "hello3 my name is 5joey"
Upvotes: 4