I am not new to R, but I am referring to a new regex.
A similar question can be found in here .
An example is the use of
> strsplit("UK, USA, Germany", ", ") [[1]] [1] "UK" "USA" "Germany"
but i want to get
[[1]] [1] "UK, USA" "Germany"
Another example is
> strsplit("London, Washington, DC, Berlin", ", ") [[1]] [1] "London" "Washington" "DC" "Berlin"
and i want to get
[[1]] [1] "London, Washington, DC" "Berlin"
Specifically, Washington, DC should not be divided into two parts, and should only be divided by the last comma , not each comma.
One viable way, I think, is to replace the last comma with something else, like
$,
then use
strsplit()
to split the string into the one you replaced (make sure it's unique!), but I'm happier if you can handle the problem using the built-in function directly.
So how can I do this? many thanks
string split r comma
Jiqing huang
source share