如何根据字符串中包含的每个大写字母拆分字符串。在互联网上找不到任何帮助。
a<-"MiXeD"
b<-"ServiceEventId"
I would like to get
a<-c("Mi", "Xe", "D")
b<-c("Service", "Event", "Id")
如何根据字符串中包含的每个大写字母拆分字符串。在互联网上找不到任何帮助。
a<-"MiXeD"
b<-"ServiceEventId"
I would like to get
a<-c("Mi", "Xe", "D")
b<-c("Service", "Event", "Id")
这里有一种选项,使用一个向后断言和一个向前断言来查找(然后在其处分割)紧随大写字母的字符间空格。要了解为什么需要同时使用向前和向后断言(而不仅仅是向前断言),请参阅此问题及其答案。
f <- function(x) {
strsplit(x, "(?<=.)(?=[[:upper:]])", perl=TRUE)
}
f(a)
# [[1]]
# [1] "Mi" "Xe" "D"
f(b)
# [[1]]
# [1] "Service" "Event" "Id"
regmatches
改编版 - regmatches(d,gregexpr("([[:upper:]]|^)([^[:upper:]]+|$)",d))
。 - thelatemailstr_extract_all
函数:library(stringr)
str_extract_all(x, "[A-Z][a-z]*")
或者
str_extract_all(x, "[A-Z][a-z]*|[a-z]+")
str_extract_all(x, "[A-Z][a-z]*|[a-z]+")
- Avinash Raj