Reputation: 665
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".
Upvotes: 1
Views: 272
Reputation: 269654
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"
From the question the input is
mystr <- c("./10g/13.9264.csv", "./6g/62.0544.csv")
Upvotes: 1
Reputation: 21400
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"
Upvotes: 1
Reputation: 5673
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"
Upvotes: 2
Reputation: 521289
You could sub()
here with a capture group:
mystr <- c("./10g/13.9264.csv", "./6g/62.0544.csv")
sub(".*/([^/]+)/.*", "\\1", mystr)
[1] "10g" "6g"
Upvotes: 4