1

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".

4 Answers 4

2

You can try sub

sub(".*_A0*(.*)_.*","\\1",values)
#[1] "9"  "15"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! But Isn't there a stringr function I could use to do it?
1

You can use lookbehind pattern -

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

Comments

1

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+)")

1 Comment

Isn't there a way to directly extract the number? something like: "_A0*(\d+)"?
1
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.