2

I am trying to split like: TwitterHashtagIsVeryCOMPLICATED

The string after being splited like: Twitter Hashtag Is Very COMPLICATED

Could I use regex to do that?

Thanks so much for any advices.

Hoa Vu
  • 2,865
  • 4
  • 25
  • 33

2 Answers2

5

This should work:

str.split("(?<=[a-z])(?=[A-Z])")

The idea is to use zero-length lookbehind for a lowercase letter, and zero-length lookahead for the uppercase letter. This construct would match only at the "word breaks" in camel case strings.

Here is a demo on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Perfect solution for ASCII ;) I don't know Twitter that much, can you use other things than ASCII letters in a twitter "id"? – fge Jul 07 '13 at 13:00
  • @dasblinkenlight Thank you very much. It works and very intuitive with demo. – Hoa Vu Jul 07 '13 at 13:01
2

Edit strongly inspired by dasblinkenlight's answer (+1 for that). I only change to Unicode categories here for Unicode support:

String test = "TwitterHashtagIsVeryCOMPLICATED";
for (String splitted: test.split("(?<=\\p{Ll})(?=\\p{Lu})")) {
    System.out.println(splitted);
}

Output:

Twitter
Hashtag
Is
Very
COMPLICATED
Mena
  • 47,782
  • 11
  • 87
  • 106
  • 1
    @HoaVu you're welcome. As fge mentions, I'm not sure whether Unicode characters are fully supported in hash tags. If they were, you might consider using Unicode categories instead of lowercase/uppercase ASCII character classes. – Mena Jul 07 '13 at 13:26
  • Thank you. Could you tell me any document that I can learn regex and the tool to test it so I can master regex by myself, rather than posting so much here :). – Hoa Vu Jul 07 '13 at 13:32
  • @HoaVu [this](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html) is my personal bible for Java regex reference. I recommend you peruse it. [This](http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html) is the javadoc for the `Matcher` class - very useful as well. Finally [this](http://docs.oracle.com/javase/tutorial/essential/regex/) is the Java regex essentials page. If you need more tutorials/examples, just google "java regex", there's plenty of really useful stuff. Good luck! – Mena Jul 07 '13 at 13:42