str_replace 不会替换所有出现的地方,但 gsub 可以吗?
str_replace doesn't replace all occurrences, but gsub does?
我正在尝试从如下所示的字符串中删除括号。
library(stringr)
x <- "(Verhoeff,1937)"
str_replace(string = x, pattern = "(\()|(\))", replacement = "")
[1] "Verhoeff,1937)"
gsub(pattern = "(\()|(\))", replacement = "", x = x)
[1] "Verhoeff,1937"
str_replace
好像没有找到右括号?
有什么想法吗?
它只匹配第一次出现,而 gsub
匹配所有。使用 str_replace_all
代替:
str_replace(string = "aa", pattern = "a", replacement = "b") # only first
str_replace_all(string = "aa", pattern = "a", replacement = "b") # all
我正在尝试从如下所示的字符串中删除括号。
library(stringr)
x <- "(Verhoeff,1937)"
str_replace(string = x, pattern = "(\()|(\))", replacement = "")
[1] "Verhoeff,1937)"
gsub(pattern = "(\()|(\))", replacement = "", x = x)
[1] "Verhoeff,1937"
str_replace
好像没有找到右括号?
有什么想法吗?
它只匹配第一次出现,而 gsub
匹配所有。使用 str_replace_all
代替:
str_replace(string = "aa", pattern = "a", replacement = "b") # only first
str_replace_all(string = "aa", pattern = "a", replacement = "b") # all