I am trying to use RegEx.Replace to convert a string into Pascal case. RegEx is not necessary, but I thought that maybe it'll be easier. Here are some example test cases I'm trying to convert:
simple simon says => SimpleSimonSays
SIMPLE SIMON SaYs => SimpleSimonSays
simple_simon_says => SimpleSimonSays
simple simon says => SimpleSimonSays
simpleSimonSays => SimpleSimonSays
simple___simon___ says => SimpleSimonSays
The method I currently have doesn't use RegEx and it works correctly on 4 of the 5 examples above:
internal static string GetPascalCaseName(string name)
{
string s = System.Globalization.CultureInfo.CurrentCulture.
TextInfo.ToTitleCase(name.ToLower()).Replace(" ", "").Replace("_", "");
return s;
}
The one example that fails is simpleSimonSays. It currently returns Simplesimonsays instead of SimpleSimonSays. How can I make this work on all 4 scenarios?
EDIT
So basically, words are distinguished if there are spaces seperating them, or underscores, or whenever an upper-case character is reached. Also, multiple spaces and/or multiple underscores should be treated as one. Basically spaces and underscores should just be ignored and used as a signal that the next letter should be a capital letter. Like this:
simple_____simon___ says => SimpleSimonSays
simpleSimonSays, there is no boundary to extract a case separation. So, unless you're using natural language processing, regex isn't going to ever do that.\b([^\W_]+)(?:[ _]*([^\W_]+))*\band use Capture Collections within a delegate callback.pkrltUdrXywaT