我应该如何编写正则表达式来删除字符串中的特定单词?

时间:2021-10-14 20:48:54

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.

我一直试图想出一个合适的regrex从字符串中删除'fries'这个词。但是,它并不是很成功。

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.

理想情况下,regrex表达式应该能够删除“fries”这个词,无论是大写和/或小写,并且在下列情况下不应删除“fries”这个词。

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.

上面的一个主要缺陷是,如果正则表达式位于字符串的开头或结尾,则无法检测到“fries”这个词。

eg. 'I love fries', 'Fries is love'

例如。 '我爱炸薯条','炸薯条就是爱'

2 个解决方案

#1


3  

TRE regex engine does not support inline modifiers and to match a whole word you need to use word boundaries \b.

TRE正则表达式引擎不支持内联修饰符并匹配您需要使用单词边界的整个单词\ b。

You may use a PCRE regex if you want to use an inline case insensitive modifier (?i):

如果要使用内联不区分大小写的修饰符(?i),可以使用PCRE正则表达式:

gsub('(?i)\\bfries\\b','',string, perl = TRUE)

or a TRE regex with ignore.case =TRUE argument:

或者使用ignore.case = TRUE参数的TRE正则表达式:

gsub('\\bfries\\b','',string, ignore.case =TRUE)

#2


1  

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

\\ 将确保只替换确切的单词“fries”

#1


3  

TRE regex engine does not support inline modifiers and to match a whole word you need to use word boundaries \b.

TRE正则表达式引擎不支持内联修饰符并匹配您需要使用单词边界的整个单词\ b。

You may use a PCRE regex if you want to use an inline case insensitive modifier (?i):

如果要使用内联不区分大小写的修饰符(?i),可以使用PCRE正则表达式:

gsub('(?i)\\bfries\\b','',string, perl = TRUE)

or a TRE regex with ignore.case =TRUE argument:

或者使用ignore.case = TRUE参数的TRE正则表达式:

gsub('\\bfries\\b','',string, ignore.case =TRUE)

#2


1  

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

\\ 将确保只替换确切的单词“fries”