我正在尝试获取 R 中字符串中左括号的数量。我正在使用 str_count
来自 stringr
的函数包裹
s<- "(hi),(bye),(hi)"
str_count(s,"(")
Error in stri_count_regex(string, pattern, opts_regex = attr(pattern, : ` Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)
我希望为这个例子得到 3
最佳答案
(
是一个特殊字符。你需要逃避它:
str_count(s,"\\(")
# [1] 3
或者,假设您使用的是
stringr
,您可以使用 coll
功能:str_count(s,coll("("))
# [1] 3
关于r - 计算字符串中 "("出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42394489/