I would like to extract the first string from a vector. For example,
y<- c('london/hilss', 'newyork/hills', 'paris/jjk')
I want to get the string before the symbol"/" i.e.,
location
london
newyork
paris
I would like to extract the first string from a vector. For example,
y<- c('london/hilss', 'newyork/hills', 'paris/jjk')
I want to get the string before the symbol"/" i.e.,
location
london
newyork
paris
Your example is simple, for a more general case like
y<- c('london/hilss', 'newyork.hills', 'paris-jjk')
maybe following will do better?
stringr::str_extract(y, '\\w*')
stringr::str_extract(a, '\\b*')
A very simple approach with gsub
gsub("/.*", '', y)
[1] "london" "newyork" "paris"
This regex will also work fine.
Regex: ^[^\/]+
It will start matching from beginning of string until a /
is found.
^
outside character class []
is anchor for beginning of string.
While ^
inside character class means negated character class.