I would like to delete letters from a string, but protect specific words. Here is an example:
my.string <- "Water the 12 gold marigolds please, but not the 45 trees!"
desired.result <- "12 marigolds, 45 trees"
I tried the code below, which gave a surprising result. I thought ()
would protect whatever it contained. Instead, just the opposite happened. Only the words within ()
were deleted (plus the !
).
gsub("(marigolds|trees)\\D", "", my.string)
# [1] "Water the 12 gold please, but not the 45 "
Here is an example with a longer string:
my.string <- "Water the 12 gold marigolds please, but not the 45 trees!, The 7 orange marigolds are fine."
desired.result <- "12 marigolds, 45 trees, 7 marigolds"
gsub("(marigolds|trees)\\D", "", my.string)
Returns:
[1] "Water the 12 gold please, but not the 45 , The 7 orange are fine."
Thank you for any advice. I prefer a regex
solution in base R
.
You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement. The following example removes all commas from a string.
To remove everything before the first occurrence of the character '-' in a string, pass the character '-' as a separator in the partition() function. Then assign the part after the separator to the original string variable. It will give an effect that we have deleted everything before the character '-' in a string.
Using word boundary, negative look-ahead assertion.
> my.string <- "Water the 12 gold marigolds please, but not the 45 trees!"
> gsub("\\b(?!marigolds\\b|trees\\b)[A-Za-z]+\\s*", "", my.string, perl=TRUE)
[1] "12 marigolds , 45 trees!"
> gsub("\\b(?!marigolds\\b|trees\\b)[A-Za-z]+\\s*|!", "", my.string, perl=TRUE)
[1] "12 marigolds , 45 trees"
An other way with a capturing group:
my.string <- "Water the 12 gold marigolds please, but not the 45 trees!, The 7 orange marigolds are fine."
gsub("(?i)\\b(?:(marigolds|trees)|[a-z]+)\\b\\s*|[.?!]", "\\1", my.string, perl=TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With