Search code examples
rregexstringi

R - How to make a stringi str_extract pattern parametric in a loop


I have strings that are segmented by forward-slashes, I am trying to generate this using a loop so I need to parametrize the regex so that I can use it inside a loop. I have 7 levels:

I want to extract the followings using the regex and stringi:

A
A/268
A/268/200
A/268/200/300
A/268/200/300/400

Here's what I have:

n=3
str_extract("A/268/200/300/400/500","(.*?/){n}"


str_extract("A/268/200/300/400/500","(.*?/){3}"

Solution

  • We can use glue::glue to interpolate the values

    n <- 3
    pat <- as.character(glue::glue("(.*?/){<-n-1->}([^/]+)", 
                   .open = "<-", .close = "->"))
    pat
    #[1] "(.*?/){2}([^/]+)"
    library(stringr)
    str_extract("A/268/200/300/400/500", pat)
    #[1] "A/268/200"
    

    If we need it as a loop

    v1 <- 1:7
    lst1 <- vector('list', length(v1))
    for(i in v1) {
       tmppat <- as.character(glue::glue("(.*?/){<-i-1->}([^/]+)",
                       .open = "<-", .close = "->"))
       lst1[[i]] <- str_extract("A/268/200/300/400/500", tmppat)
     }
    
    
    
    
    
    head(lst1, 5)
    #[[1]]
    #[1] "A"
    
    #[[2]]
    #[1] "A/268"
    
    #[[3]]
    #[1] "A/268/200"
    
    #[[4]]
    #[1] "A/268/200/300"
    
    #[[5]]
    #[1] "A/268/200/300/400"