How to split a string based on each upper case letter it contains. Couldn't find any help from internet.
a<-"MiXeD"
b<-"ServiceEventId"
I would like to get
a<-c("Mi", "Xe", "D")
b<-c("Service", "Event", "Id")
How to split a string based on each upper case letter it contains. Couldn't find any help from internet.
a<-"MiXeD"
b<-"ServiceEventId"
I would like to get
a<-c("Mi", "Xe", "D")
b<-c("Service", "Event", "Id")
Here's one option, which uses one lookbehind and one lookahead assertion to find (and then split at) intercharacter spaces that are immediately followed by an uppercase letter. To learn why both a lookahead and a lookbehind assertion are needed (i.e. not just a lookahead assertion) see this question and its answers.
f <- function(x) {
strsplit(x, "(?<=.)(?=[[:upper:]])", perl=TRUE)
}
f(a)
# [[1]]
# [1] "Mi" "Xe" "D"
f(b)
# [[1]]
# [1] "Service" "Event" "Id"
Use str_extract_all
from the stringr package:
library(stringr)
str_extract_all(x, "[A-Z][a-z]*")
or
str_extract_all(x, "[A-Z][a-z]*|[a-z]+")