some previous questions have been asked on this topic, but they don't seem to include the case when a string contains multiple instances of the same delimiter.
How to extract substring between patterns "_" and "." in R
Extracting a string between other two strings in R
Extract a string between patterns/delimiters in R
The problem I am facing is the following. Say we have a vector like this:
vec <- c("Europe/Germany/Berlin/Mitte",
"Europe/Germany/Berlin/Charlottenburg",
"Europe/Croatia/Zagreb/Gornji Grad",
"Europe/Croatia/Zagreb/Donji Grad")
Can you provide me with the following two functions:
The output of the first function should be:
c("Germany", "Germany", "Croatia", "Croatia")
And the output of the second function should be:
c("Berlin", "Berlin", "Zagreb", "Zagreb")
I don't understand how the answers from previous questions apply when the delimiter /
appears more than once in the string and how can I specify which of the pieces I want.