Search code examples
rregexstring-matchingcapturestringr

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?


Solution

  • 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))