Splitting CamelCase With Regular Expressions

On my new project, I needed to split a method name up into its constituent parts.

For Ruby programmers, this is easy: just split on underscores. For us .NET programmers, we need something a little fancier since we like to SquashOurMethodNamesTogetherLikeThis.

Here’s a little regular expression that can do exactly that:

(?<!^)(?=[A-Z])

That basically says to match right before every capital letter unless the capital letter is at the beginning of the string.

You can use the Regex.Split method to do the actual work of turning the method name into a string array:

Regex splitter = new Regex(@"(?<!^)(?=[A-Z])");
string[] words = splitter.Split(methodName);

My friend, Michael Kennedy, demoed in class recently how to use LINQPad to test regular expressions. It came in really handy while working on this:

linqpad-with-regex

I really need to click that “Activate Autocompletion” button so I can give Joseph Albahari the money he deserves for creating such a useful tool.

By the way, I used to think that “camel case” was for words like “camelCase” and “pascal case” was for words like “PascalCase”, but Wikipedia doesn’t make that distinction.

5 thoughts on “Splitting CamelCase With Regular Expressions”

  1. Your thinking about camelCasing and PascalCasing is correct. If Wikipedia (the mother of non-authoritative sources) contradicts that, then, QED, it’s incorrect. See Microsoft for official definitions (camelCase/PascalCasing).

    Please do not corrupt the world by waffling. camelCase is camelCase and PascalCase is PascalCase.

  2. I just tried your regex… it works perfectly in Java…

    String s = “loremIpsum”;
    words = s.split(“(? V / A / L / U / E
    2) eclipseRCPExt -> eclipse / R / C / P / Ext

    To my mind, the result shoud be:
    1) VALUE
    2) eclipse / RCP / Ext

    In other words, given n uppercase chars, if there are follwed by lower case, it should be (n-1 chars) / (n-th char + lower chars)

    if there are not at the end: (n chars).

    If you have any idea with a regex…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>