Alexey Ferapontov
Alexey Ferapontov

Reputation: 5169

R: Extract N characters after M regex matches in one string

I'm having difficulties in extracting exactly N characters after all regex matches in one string. E.g. N=3, and regex match is an opening bracket "(", and may be paste an aux word in between if M >= 1.

x1 = "use actual (Mon 3)|(Tue 4)"
x2 = "use actual (Mon 10)"
x3 = "use actual"

Desired outputs are:

"Mon and Tue"
"Mon"
""

I started with gsub( ".*\\(", "", c("use actual (Mon 3)|(Tue 4)")) and then tried to follow R extract part of string but got lost in ? and wildcards. Thanks!

Upvotes: 3

Views: 1711

Answers (1)

akrun
akrun

Reputation: 886998

try

 library(stringr)
 sapply(str_extract_all(x, perl('(?<=\\()[A-Za-z]{3}')),
             paste, collapse=' and ')
 #[1] "Mon and Tue"         "Mon"                 ""                   
 #[4] "Mon and Tue and Wed" "Mon"   

data

 x4 <- "use actual (Mon 3)|(Tue 4)|(Wed 5)"
 x5 <- "foo (Mond bar)"
 x <- c(x1, x2, x3, x4, x5)

Upvotes: 4

Related Questions