--- title: "Splitting Pascal/Camel Case with RegEx Enhancements" date: 2010-09-11T21:03:15.087-05:00 slug: splitting-pascalcamel-case-with-regex-enhancements published: true --- In [Jon Galloway’s](http://weblogs.asp.net/jgalloway/) [Splitting Camel Case with RegEx](http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx) blog post, he introduced a simple regular expression replacement which can split “ThisIsInPascalCase” into “This Is In Pascal Case”.  Here’s the original code:output = System.Text.RegularExpressions.Regex.Replace( input, "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim(); Simple and effective.  Matches any capital letters and inserts a space before them.  But there’s room for improvement.  First, the call to String.Trim() to remove any spaces potentially added if the first letter is uppercase – this can be handled with a [“Match if prefix is absent” group](http://msdn.microsoft.com/en-us/library/az24scfc.aspx#grouping_constructs) containing the “beginning of line” character ^.  This prevents any matches from occurring on the first character, which eliminates the need for the String.Trim() call.  The formal name for this grouping construct is “Zero-width negative lookbehind assertion”, but just think of it as “if you see what’s in here, don’t match the next thing”. (?