R gsub 多个条件

R gsub multiple conditions

我正在尝试对一组字符串使用 gsub,这些字符串的措辞可能略有不同;

I went to the store last night
I went to the park yesterday
I went to starbucks this morning

我需要使用 gsub 来替换 'I went to...',但有时会有 'the',有时不会

类似这样,但下面的将无法正常工作

gsub('i went to [the|a-z]','REPLACED',string)

REPLACED last night
REPLACED yesterday
REPLACED this morning

尝试:

gsub("I went to (the )?[a-z]", "REPLACED", string)

您可以将 stringr 包与以下正则表达式一起使用(使用 ^ 锚定在字符串的前面):

library(stringr)
sentences <- c("I went to the store last night",
               "I went to the park yesterday",
               "I went to starbucks this morning")
str_replace(sentences, "^I went to( the)?", "REPLACED")
# [1] "REPLACED store last night"       "REPLACED park yesterday"        
# [3] "REPLACED starbucks this morning"

如果在同一个字符串中有多个要替换的实例,您可能需要省略 ^ 并使用 str_replace_all()