using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(\b(?'first_word'[A-Z](?!([A-Z0-9]*\b|[[:alnum:]]*_)(?# the word isn't in all caps and doesn't contain underscores))(?:[a-z0-9]*)))|(\G(?'trailing_word'[A-Z][a-z0-9]*))";
string substitution = @"\L${first_word}${trailing_word:+_\L${trailing_word}:}";
string input = @"PascalCase
Pascal
PascalCaseCase
Pascal684Case
Pas45calCase
PascalCase6523Case
PascalCaseCaseCase
PPascal
PascalCCase
camelCase
// some random commentary with Capitalized and SCREAMING letters
SCREAMING_SNAKE_CASE
Pascal_Snake_Case
";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx