stringr with groups in R
Capture groups, named or numbered, extracted into a matrix or a tibble.
library(stringr)
library(tibble)
lines <- c("INFO serve: started", "WARN db: slow query", "nonsense")
pattern <- "^(\\w+) (\\w+): (.*)$"
matches <- str_match(lines, pattern)
print(matches)
print(as_tibble(matches[, -1], .name_repair = ~ c("level", "logger", "msg")))
print(str_count(lines, "\\w+"))
print(str_locate(lines, "serve"))
print(str_detect("Typestar", regex("typestar", ignore_case = TRUE)))
How it works
str_matchreturns the whole match then each group.regex()carries options like case insensitivity.str_countandstr_locateanswer the other questions.
Keywords and builtins used here
as_tibbleclibraryprintregexstr_countstr_detectstr_locatestr_match
The run, in numbers
- Lines
- 13
- Characters to type
- 404
- Tokens
- 101
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 61 seconds.
Step 3 of 3 in stringr, step 7 of 10 in Functional & text tools.