计算字符串中“("的出现次数 [英] count the number of occurrences of "(" in a string
问题描述
我正在尝试获取 R 中字符串中左括号的数量.我正在使用 stringr
包中的 str_count
函数
s<- "(hi),(bye),(hi)"str_count(s,"(")
<块引用>
stri_count_regex(string, pattern, opts_regex = attr(pattern,: ` 正则表达式模式中嵌套的括号不正确.(U_REGEX_MISMATCHED_PAREN)
我希望在这个例子中得到 3 个
(
是一个特殊字符,需要转义:
str_count(s,"\\(")# [1] 3
或者,如果您使用的是 stringr
,您可以使用 coll
函数:
str_count(s,coll("("))# [1] 3
I am trying to get the number of open brackets in a character string in R. I am using the str_count
function from the stringr
package
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)
I am hoping to get 3 for this example
(
is a special character. You need to escape it:
str_count(s,"\\(")
# [1] 3
Alternatively, given that you're using stringr
, you can use the coll
function:
str_count(s,coll("("))
# [1] 3
这篇关于计算字符串中“("的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!