-2

I need to split a camelCased text containing only lowerCase and UpperCase letters. How to do it using regular expression?

Example text: ThisTextIsToBeSplitted

Output: This Text Is To Be Splitted

optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37
  • What should be the output for "ThisTEXTIsToBeSplitted"? – eekboom Sep 25 '17 at 11:42
  • 2
    Please check this post: https://stackoverflow.com/questions/7593969/regex-to-split-camelcase-or-titlecase-advanced Good Explanation – Longer04 Sep 25 '17 at 11:49
  • You can see [my answer](https://stackoverflow.com/a/46409373/3600709) to the post above by @Longer04. The regex works in multiple languages, not just Java and doesn't depend on negative lookbehinds. I find it's much easier to read than most of the regexes that have been shared on both this post and the one linked as well, but that's my own personal opinion and is definitely bias – ctwheels Sep 25 '17 at 15:59

4 Answers4

3

I would offer the following solution, which preserves acronyms (e.g. ABC), which the other answers do not:

String input = "ThisTextWithInitialABCIsToBeSplitted";
String[] parts = input.split("((?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z]))");
for (String part : parts) {
    System.out.println(part);
}

Output:

This
Text
With
Initial
ABC
Is
To
Be
Splitted

The logic of the split is to use lookarounds which assert, but do not consume. A split happens on either of the following two conditions:

(?<=[a-z])(?=[A-Z])
(?<=[A-Z])(?=[A-Z][a-z])

The first condition is when we are at a position immediately preceded by a lowercase letter and immediately proceeded by a capital letter. But with this rule alone, the string InitialABCIs would split to this:

Intitial
ABCI
s

To fix this, I added a second condition which splits when the preceding letter be capital, followed by one more capital and a lowercase. This allows us to separate the true start of the next camelcase word.

Demo here:

Rextester

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

you can use the regex

(?=[A-Z])

see the regex101 demo, online compiler

marvel308
  • 10,288
  • 1
  • 21
  • 32
1
String s = "ThisTextIsToBeSplitted";
System.out.println(Arrays.asList(s.split("(?=[A-Z])")));

works fine. My output is:

[This, Text, Is, To, Be, Splitted]

for the example of Stephen, the output is [This, T, E, X, T, Is, To, Be, Splitted] because it spits at every upper case letter

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
0

I've done with the expression: (?=\\p{Upper})

optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37