Extract numeric value from a file name string

I don't quite understand regular expressions yet, and for this project I don't have time to learn it (but any great resources would be appreciated). Say I have this file name as a string:

string <- "2019data/ABC19_XY_10001/finit1/ABC19_XY_10001_FAST.csv"

I want to extract the number 10001. How do I do that in the above string?

Thanks so much!

Here is a solution using a minimal amount of regular expressions. The basename function is part of base R and it extracts just the file name from a full path.

string <- "2019data/ABC19_XY_10001/finit1/ABC19_XY_10001_FAST.csv"
library(stringr)
DashNumberDash <- str_extract(basename(string), "_\\d+_")
DashNumberDash
#> [1] "_10001_"
JustNumber <- str_remove_all(DashNumberDash, "_")
JustNumber
#> [1] "10001"

Created on 2023-12-20 with reprex v2.0.2

What if I wanted to extract the number and the final word FAST, like 10001_FAST?

Try this:

> library(stringr)
> string <- "2019data/ABC19_XY_10001/finit1/ABC19_XY_10001_FAST.csv"
> str_match(string, '_(\\d+_\\w+).csv')[,2]
[1] "10001_FAST"