Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting CamelCase in R

Is there a way to split camel case strings in R?

I have attempted:

string.to.split = "thisIsSomeCamelCase"
unlist(strsplit(string.to.split, split="[A-Z]") )
# [1] "this" "s"    "ome"  "amel" "ase" 
like image 637
kmace Avatar asked Dec 06 '11 21:12

kmace


4 Answers

I think my other answer is better than the follwing, but if only a oneliner to split is needed...here we go:

library(snakecase)
unlist(strsplit(to_parsed_case(string.to.split), "_"))
#> [1] "this"  "Is"    "Some"  "Camel" "Case" 
like image 132
Taz Avatar answered Sep 24 '22 15:09

Taz


string.to.split = "thisIsSomeCamelCase"
gsub("([A-Z])", " \\1", string.to.split)
# [1] "this Is Some Camel Case"

strsplit(gsub("([A-Z])", " \\1", string.to.split), " ")
# [[1]]
# [1] "this"  "Is"    "Some"  "Camel" "Case" 

Looking at Ramnath's and mine I can say that my initial impression that this was an underspecified question has been supported.

And give Tommy and Ramanth upvotes for pointing out [:upper:]

strsplit(gsub("([[:upper:]])", " \\1", string.to.split), " ")
# [[1]]
# [1] "this"  "Is"    "Some"  "Camel" "Case" 
like image 16
IRTFM Avatar answered Nov 02 '22 09:11

IRTFM


Here is one way to do it

split_camelcase <- function(...){
  strings <- unlist(list(...))
  strings <- gsub("^[^[:alnum:]]+|[^[:alnum:]]+$", "", strings)
  strings <- gsub("(?!^)(?=[[:upper:]])", " ", strings, perl = TRUE)
  return(strsplit(tolower(strings), " ")[[1]])
}

split_camelcase("thisIsSomeGood")
# [1] "this" "is"   "some" "good"
like image 11
Ramnath Avatar answered Nov 02 '22 09:11

Ramnath


Here's an approach using a single regex (a Lookahead and Lookbehind):

strsplit(string.to.split, "(?<=[a-z])(?=[A-Z])", perl = TRUE)

## [[1]]
## [1] "this"  "Is"    "Some"  "Camel" "Case" 
like image 7
Tyler Rinker Avatar answered Nov 02 '22 11:11

Tyler Rinker