Ishan Mehta
Ishan Mehta

Reputation: 326

How to extract number from file name?

I have a list of file names in R:

> dput(file_names)
c("EXACT_46401#1.stats.json", "EXACT_46401#11.stats.json", "EXACT_46401#12.stats.json", 
"EXACT_46401#13.stats.json", "EXACT_46401#14.stats.json", "EXACT_46401#15.stats.json", 
"EXACT_46401#16.stats.json", "EXACT_46401#7.stats.json", "EXACT_46401#18.stats.json", 
"EXACT_46401#19.stats.json")

As you can see there are some filenames with a single digit number and some with a double digit number, so I cannot extract by selecting the correct substring. How do I extract the number from these filenames as a vector?

Thanks.

Upvotes: 1

Views: 101

Answers (1)

akrun
akrun

Reputation: 887088

We may capture the digits (\\d+) after the # and that precedes the . and specify the capture group number as 1

library(stringr)
str_extract(file_names, "#(\\d+)\\.", group = 1)
[1] "1"  "11" "12" "13" "14" "15" "16" "7"  "18" "19"

NOTE: If it needs to be integer type, wrap as.integer around the str_extract

Upvotes: 3

Related Questions