count the number of occurrences of “(” in a string

别来无恙 提交于 2019-11-27 07:36:56

问题


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


回答1:


( 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



回答2:


If you want to do it in base R you can split into a vector of individual characters and count the "(" directly (without representing it as a regular expression):

> s<- "(hi),(bye),(hi)"
> chars <- unlist(strsplit(s,""))
> length(chars[chars == "("])
[1] 3



回答3:


You could also use gregexpr along with length in base R:

sum(gregexpr("(", s, fixed=TRUE)[[1]] > 0)
[1] 3

gregexpr takes in a character vector and returns a list with the starting positions of each match. I added fixed=TRUE in order to match literals.length will not work because gregexpr returns -1 when a subexpression is not found.


If you have a character vector of length greater than one, you would need to feed the result to sapply:

# new example
s<- c("(hi),(bye),(hi)", "this (that) other", "what")
sapply((gregexpr("(", s, fixed=TRUE)), function(i) sum(i > 0))
[1] 3 1 0


来源:https://stackoverflow.com/questions/42394489/count-the-number-of-occurrences-of-in-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!