Home > Back-end >  extract pattern using stringr
extract pattern using stringr

Time:01-23

I have a vector of strings with the following format "IN_D44_A09_ET" and I would like to extract the number 9 using the stringr package.

I have been trying to solve it using str_extract(), but I don't get how to formulate the pattern.

values <- c("IN_D44_A09_CT", "XE_D34_A15_ET")
str_extract(values, "_A(\\d )")

This pattern extracts "_A09" and "_A15" but what I want is "9" and "15".

CodePudding user response:

You can try sub

sub(".*_A0*(.*)_.*","\\1",values)
#[1] "9"  "15"

CodePudding user response:

You can use lookbehind pattern -

as.integer(stringr::str_extract(values, '(?<=A)\\d '))
#[1]  9 15

CodePudding user response:

One way could be to use str_extract twice. In the first str_exract whatever output you are getting, store it as a vector and run str_extract again to get the desired output.

Here's how you could do it:

values <- c("IN_D44_A09_CT", "XE_D34_A15_ET")
temp <- str_extract(values, "A(\\d )")
str_extract(temp, "(\\d )")

CodePudding user response:

library(stringr)

values <- c("IN_D44_A09_CT", "XE_D34_A15_ET")

str_match(values, 'A(\\d )')[, 2]
#> [1] "09" "15"

Created on 2022-01-22 by the reprex package (v2.0.1)

If we want to remove the zeros:

library(stringr)

values <- c("IN_D44_A00090_CT", "XE_D34_A0015_ET")

str_match(values, 'A(\\d )')[, 2] %>% str_replace('^0 ', '')
#> [1] "90" "15"

Created on 2022-01-22 by the reprex package (v2.0.1)

  •  Tags:  
  • Related