如何根据字符串包含的每个大写字母拆分字符串。在互联网上找不到任何帮助。
a<-"MiXeD"
b<-"ServiceEventId"我想要一个
a<-c("Mi", "Xe", "D")
b<-c("Service", "Event", "Id")发布于 2016-01-14 08:16:03
这里有一个选项,它使用一个后视和一个前视断言来查找(然后拆分)紧跟大写字母的字符间空格。要了解为什么同时需要前视和后视断言(即不仅仅是前视断言),请使用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" 发布于 2016-01-14 08:27:49
使用stringr包中的str_extract_all:
library(stringr)
str_extract_all(x, "[A-Z][a-z]*")或
str_extract_all(x, "[A-Z][a-z]*|[a-z]+")https://stackoverflow.com/questions/34779435
复制相似问题