str_replace不能代替所有实例,但是gsub可以吗? [英] str_replace doesn't replace all occurrences, but gsub does?
本文介绍了str_replace不能代替所有实例,但是gsub可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从下面的字符串中删除括号.
I am trying to remove brackets from a string like the one below.
library(stringr)
x <- "(Verhoeff,1937)"
str_replace(string = x, pattern = "(\\()|(\\))", replacement = "")
[1] "Verhoeff,1937)"
gsub(pattern = "(\\()|(\\))", replacement = "", x = x)
[1] "Verhoeff,1937"
str_replace
似乎找不到右括号?
有什么想法吗?
str_replace
doesn't seem to find the closing bracket?
Any ideas why?
推荐答案
它仅与第一次出现匹配,而gsub
则全部匹配.使用str_replace_all
代替:
It only matches the first occurency, whereas gsub
does it all. Use str_replace_all
instead:
str_replace(string = "aa", pattern = "a", replacement = "b") # only first
str_replace_all(string = "aa", pattern = "a", replacement = "b") # all
这篇关于str_replace不能代替所有实例,但是gsub可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文