Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of occurrences at end of string

Tags:

r

I want to count how many commas are at the end of a string with a regex:

x <- c("w,x,,", "w,x,", "w,x", "w,x,,,")

I'd like to get:

[1] 2 1 0 3

This gives:

library(stringi)
stringi::stri_count_regex(x, ",+$")
## [1] 1 1 0 

Because I'm using a quantifier but don't know how to count actual number of times single character was repeated at end.

like image 486
Tyler Rinker Avatar asked Mar 15 '23 10:03

Tyler Rinker


1 Answers

The "match.length" attribute within the regexpr seem to get the job done (-1 is used to distinguish no match from zero-width matches such as lookaheads)

attr(regexpr(",+$", x), "match.length")
## [1] 2  1 -1  3

Another option (with contribution from @JasonAizkalns) would be

nchar(x) - nchar(gsub(",+$", "", x))
## [1] 2 1 0 3

Or using stringi package combined with nchar while specifying , keepNA = TRUE (this way no matches will be specified as NAs)

library(stringi)
nchar(stri_extract_all_regex(x, ",+$"), keepNA = TRUE)
## [1] 2  1 NA  3
like image 71
David Arenburg Avatar answered Mar 24 '23 17:03

David Arenburg