Home > other >  Extract a number from a string which precedes a phrase in R
Extract a number from a string which precedes a phrase in R

Time:06-01

I am in R and would like to extract a two digit number 38y from the following string:

"/Users/files/folder/file_number_23a_version_38y_Control.txt"

I know that _Control always comes after the 38y and that 38y is preceded by an underscore. How can I use strsplit or other R commands to extract the 38y?

CodePudding user response:

You could use

regmatches(x, regexpr("[^_] (?=_Control)", x, perl = TRUE))
# [1] "38y"

or equivalently

stringr::str_extract(x, "[^_] (?=_Control)")
# [1] "38y"

CodePudding user response:

Using gsub.

gsub('.*_(.*)_Control.*', '\\1', x)
# [1] "38y"

See demo with detailed explanation.

CodePudding user response:

A possible solution:

library(stringr)

text <- "/Users/files/folder/file_number_23a_version_38y_Control.txt"

str_extract(text, "(?<=_)\\d \\D(?=_Control)")

#> [1] "38y"

You can find an explanation of the regex part at:

https://regex101.com/r/PQSZHX/1

  •  Tags:  
  • r
  • Related