Domanda

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?

È stato utile?

Soluzione

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))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top