I have strings like this one x <- "123/456/789"
. I want to do three different things
/
, that is, 123
/
: 456
/
I know there are some functions like gsub
and packages like stingr
but I did not find something useful
You could get it to work using gsub
and str_extract
from {stringr}
:
# Get 123
gsub("/\\d*", "", x)
# Get 456
gsub("/", "", stringr::str_extract(x, "/\\d*/"))
# Get 789
gsub("\\d*/", "", x)
This results in:
[1] "123"
[1] "456"
[1] "789"
In the RegEx, \\d*
indicates one or more digits, but if your word consists of other characters you could also use a different indicator.