Regex - PascalCase to lower case with underscores

Use String.ToLower for the lowercase.

For the regex, the following seems to work:

((?<=.)[A-Z][a-zA-Z]*)|((?<=[a-zA-Z])\d+)

combined with the replacement expression:

_$1$2

Here's a full sample:

string strRegex = @"((?<=.)[A-Z][a-zA-Z]*)|((?<=[a-zA-Z])\d+)";
Regex myRegex = new Regex(strRegex, RegexOptions.Multiline);
string strTargetString = @"Is24Hour" + "\n" + 
    @"Is512" + "\n" + @"A12Hour4" + "\n" + 
    @"23AHourDay12" + "\n" + @"An8DAY512";

string strReplace = @"_$1$2";

return myRegex.Replace(strTargetString, strReplace).ToLower();

Tags:

C#

Regex