R中的Gsub函数

时间:2016-03-30 16:59:06

标签: r gsub

我有以下句子:

trying <- 'Happy CNY to You <-U+2661> Abundance BLESSING to you and your family! <-U+2661> happy and blessed year ahead! '

我想删除这句话中的所有<.....>个实体。我使用了以下功能:

comment = gsub(pattern = "<.*>", replacement = "", x= trying)

然而,我的回复是:

"Happy CNY to You happy and blessed year ahead! "

我可以知道如何编辑代码,以便我能够拥有以下内容:

"Happy CNY to You Abundance BLESSING to you and your family! happy and blessed year ahead! "

1 个答案:

答案 0 :(得分:5)

因为正则表达式匹配是贪婪的。您可以在表达式中放置?

comment = gsub(pattern = "<.*?>", replacement = "", x= trying)