Number at the end of a line

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

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

I would like to receive:

[1] 2 1 0 3

This gives:

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

Because I use a quantifier, but I don’t know how to count the actual number of times that one character was repeated at the end.

+4
source share
1 answer

Attribute "match.length"in regexprseems to do the job (-1 is used to distinguish match from zero-width matches such as lookaheads)

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

Another option (with input from @JasonAizkalns) would be

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

Or using the package stringiin conjunction with ncharwhen specifying , keepNA = TRUE(thus, no matches will be specified as NAs)

library(stringi)
nchar(stri_extract_all_regex(x, ",+$"), keepNA = TRUE)
## [1] 2  1 NA  3
+7
source

Source: https://habr.com/ru/post/1612484/


All Articles