Home > Software engineering >  Extract a part of a changeabel string
Extract a part of a changeabel string

Time:03-22

I have a simple but yet complicated question (at least for me)!

I would like to extract a part of a string like in this example:

From this string:

name <- "C:/Users/admin/Desktop/test/plots/"

To this:

name <- "test/plots/"

The plot twist for my problem that the names are changing. So its not always "test/plots/", it could be "abc/ccc/" or "m.project/plots/" and so on.

In my imagination I would use something to find the last two "/" in the string and cut out the text parts. But I have no idea how to do it!

Thank you for your help and time!

CodePudding user response:

Without regex

Use str_split to split your path by /. Then extract the first three elements after reversing the string, and paste back the / using the collapse argument.

library(stringr)

name <- "C:/Users/admin/Desktop/m.project/plots/"

paste0(rev(rev(str_split(name, "\\/", simplify = T))[1:3]), collapse = "/")

[1] "m.project/plots/"

With regex

Since your path could contain character/numbers/symbols, [^/] /[^/] /$ might be better, which matches anything that is not /.

library(stringr)

str_extract(name, "[^/] /[^/] /$")

[1] "m.project/plots/"

CodePudding user response:

With {stringr}, assuming the path comprises folders with lower case letters only. You could adjust the alternatives in the square brackets as required for example if directory names include a mix of upper and lower case letters use [.A-z] Check a regex reference for options:

name <- c("C:/Users/admin/Desktop/m.project/plots/",
          "C:/Users/admin/Desktop/test/plots/")

library(stringr)
str_extract(name, "[.a-z] /[.a-z] /$")
#> [1] "m.project/plots/" "test/plots/"

Created on 2022-03-22 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related