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"
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"
Here is one way to do it
Here is a one-liner using the
gsubfn
package'sstrapply
. The regular expression matches the beginning of the string (^
) followed by one or more lower case letters ([[:lower:]]+
) or (|
) an upper case letter ([[:upper:]]
) followed by zero or more lower case letters ([[:lower:]]*
) and processes the matched strings withc
(which concatenates the individual matches into a vector). As withstrsplit
it returns a list so we take the first component ([[1]]
) :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:]
The beginnings of an answer is to split all the characters:
Then find which string positions are upper case:
Then use that to split out each run of characters . . .
I think my other answer is better than the follwing, but if only a oneliner to split is needed...here we go:
Here's an approach using a single regex (a Lookahead and Lookbehind):