basename
will give you the part after the last slash. Then, you can split on the dot (which you have to escape with two \
)
> (name <- basename("../some/thing/foobar/foobar.happening"))
[1] "foobar.happening"
> unlist(strsplit(name, "\\."))
[1] "foobar" "happening"
Then select the first element
> unlist(strsplit(name, "\\."))[1]
[1] "foobar"
I see you actually asked for a way other than strsplit
. Here's a regular expression
> sub(".*/([^/]+)\\..*", "\\1", "../some/thing/foobar/foobar.happening")
[1] "foobar"
- It looks for zero or more occurences of anything
.*
- followed by a forward slash
/
- followed by anything that is not a forward slash
[^/]
(inside the brackets, ^
means "not"), one or more times +
.
- followed by a dot
\\.
- followed by anything zero or more times
.*
.
Then it replaces that with only the stuff inside the parenthesis [^/]+
which is everything between the forward slash and the dot. The \\1
means the stuff inside the first set of parenthesis. (there's only one set in this case, but if there were a second we could refer to it with \\2
)