Skip to content

Instantly share code, notes, and snippets.

@lamrongol
Created March 6, 2015 14:09
Show Gist options
  • Save lamrongol/d5b81d9d4b1c438d0595 to your computer and use it in GitHub Desktop.
Save lamrongol/d5b81d9d4b1c438d0595 to your computer and use it in GitHub Desktop.
Split camel case by using lookahead and lookbehind Zero-Length Assertions of regular expression
/**
* en: Split camel case by using lookahead and lookbehind Zero-Length Assertions of regular expression
* ja:キャメルケースの分割。正規表現のゼロ幅先読みを使用
*
* @param hashTag
* @return "camelCase" -> {"camel", "Case"}, "EMABiggestFans1D" -> {"EMA", "Biggest", "Fans", "1D"} , "MVDvsSSE" -> {"MVD", "SSE"}
* @see <a href="http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx#grouping_constructs">Regular Expression Language - Quick Reference</a>
* @see <a href="http://stackoverflow.com/questions/7593969/regex-to-split-camelcase-or-titlecase-advanced">java - RegEx to split camelCase or TitleCase (advanced) - Stack Overflow</a>
*/
public static String[] splitCamelCase(String hashTag) {
String[] tmp = hashTag.split("(?<=[A-Z0-9])vs?(?=[A-Z0-9])");
if (tmp.length > 1) return tmp;
return hashTag.split("(?<!(^|[A-Z0-9]))(?=[A-Z0-9])|(?<!^)(?=[A-Z][a-z])");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment