user1981275
user1981275

Reputation: 13372

How to match multiple capture groups with str_match(..., regex)

I am using str_match from the stringr package to capture text in between brackets.

library(stringr)

strs = c("P5P (abcde) + P5P (fghij)", "Glcext (abcdef)")
str_match(strs, "\\(([a-z]+)\\)")

gives me only the matches "abcde" and "abcdef". How can I capture the "fghij" as well with still using the same regex for both strings?

Upvotes: 1

Views: 3331

Answers (1)

Matthew Plourde
Matthew Plourde

Reputation: 44614

str_extract_all(strs, "\\(([a-z]+)\\)")

or as @JoshO'Brien mentions in his comment,

str_match_all(strs, "\\(([a-z]+)\\)")

This can just as easily be accomplished with base R:

regmatches(strs, gregexpr("\\(([a-z]+)\\)", strs))

Upvotes: 5

Related Questions