Reputation: 63
I have been trying to come up with a suitable regrex to remove the word 'fries' from strings. However, it hasn't been very successful.
Ideally, the regrex expression should be able to remove the word 'fries' regardless of whether is it in upper case and/ or lower case and the word 'fries' shouldn't be removed under the following cases.
frenchfries
friesislove
ilovefriesverymuch
This is what I have came up with so far
gsub('(?i)\\Wfries\\W','',string)
One major flaw with the above is that regex expression isn't able to detect the word 'fries' if it's at either the start or the end of the string.
eg. 'I love fries', 'Fries is love'
Upvotes: 0
Views: 1820
Reputation: 207
You can also try this:
gsub("\\<fries\\>",replacement = "",string ,ignore.case = TRUE)
\\<fries\\>
will make sure that only the exact word "fries" will be replaced
Upvotes: 1
Reputation: 626738
TRE regex engine does not support inline modifiers and to match a whole word you need to use word boundaries \b
.
You may use a PCRE regex if you want to use an inline case insensitive modifier (?i)
:
gsub('(?i)\\bfries\\b','',string, perl = TRUE)
or a TRE regex with ignore.case =TRUE
argument:
gsub('\\bfries\\b','',string, ignore.case =TRUE)
Upvotes: 3