I am trying to replace (technically delete) a level of folders from a vector of file path strings. The data look like this:
x<-c("d:/KeepItSimple/1234path21/WAVs/filename.wav",
"d:/TryToKeepItSimple/5678path23/WAVs/filename2.wav")
I would like to use gsub and regex to find the pattern "path" in each string and replace the string containing "path" between the two forward slashes with nothing. Essentially just remove that folder level. You can assume that the number of characters naming that folder level will always contain "path" and will always be 10 characters long (between the slashes).
After some head scratching I came up with this:
gsub(".{4}path.{2}", "", x)
It works, but it leaves me with two questions:
- Is there a better way to express/accomplish this in regex.
- How could I do it so it would look for everything to the first / before "path" and the next / after "path"?