Home > Software engineering >  Extract filename from string without extension
Extract filename from string without extension

Time:04-01

Have this as string

x <- c("MR 200 12345M123456.txt", "MR 200 12345M12345.txt")

expected output:

12345M123456
12345M12345

Any suggestions would be great

CodePudding user response:

Split on space " ", get the last item, then extract filename without extension:

tools::file_path_sans_ext(sapply(strsplit(x, " ", fixed = TRUE), tail, 1))
# [1] "12345M123456" "12345M12345" 

CodePudding user response:

Another possibility using stringr::str_extract or base::regmatches and base::regexpr.

x <- c("MR 200 1234556456M123456.txt", "MR 200 12345M12345.txt")
stringr::str_extract(x, pattern = "[1-9] M[1-9] ")
#> [1] "1234556456M123456" "12345M12345"
regmatches(x, regexpr("[1-9] M[1-9] ", x))
#> [1] "1234556456M123456" "12345M12345"

The regular expression [1-9] M[1-9] selects the letter M and all numbers before and after it. So it does not depend on how many numbers you have before or after the letter M and should work even if "12345 or 123456 are dynamic".

Example:

stringr::str_extract("MR 200 1234556456M156.txt", pattern = "[1-9] M[1-9] ")
[1] "1234556456M156"
  • Related