Home > Mobile >  How to extract text between two separators in R?
How to extract text between two separators in R?

Time:09-01

I have a vector of strings like so:

mystr <- c("./10g/13.9264.csv", "./6g/62.0544.csv")

I only want the part between the two forward slashes, i.e., "10g" and "6g".

CodePudding user response:

You could sub() here with a capture group:

mystr <- c("./10g/13.9264.csv", "./6g/62.0544.csv")
sub(".*/([^/] )/.*", "\\1", mystr)

[1] "10g" "6g"

CodePudding user response:

similar to Tim Biegeleisen, but with a lookbehind and lookahead, using srt_extract from stringr:

library(stringr)
mystr <- c("./10g/13.9264.csv", "./6g/62.0544.csv")

str_extract(mystr,"(?<=/)[^/] (?=/)")

[1] "10g" "6g" 

CodePudding user response:

More simply you can capitalize on the fact that the desired substring is one or more digits followed by literal g:

library(stringr)
str_extract(mystr, "\\d g")
[1] "10g" "6g"

CodePudding user response:

Here are a few alternatives. They use no packages and the first two do not use any regular expressions.

basename(dirname(mystr))
## [1] "10g" "6g"

read.table(text = mystr, sep = "/")[[2]]
## [1] "10g" "6g"

trimws(trimws(mystr,, "[^/]"),, "/")
## [1] "10g" "6g" 

We could also reformulate these using pipes

mystr |> dirname() |> basename()
## [1] "10g" "6g"

read.table(text = mystr, sep = "/") |> (`[[`)(2)
## [1] "10g" "6g" 

mystr |> trimws(,  "[^/]") |> trimws(, "/")
## [1] "10g" "6g"

Note

From the question the input is

mystr <- c("./10g/13.9264.csv", "./6g/62.0544.csv")
  • Related