Wednesday, July 16, 2014

Regex to Get Name Separated By Comma From PascalCase.

This is the Regex to Find the Starting of Word in PascalCase.
Finding the Beging of each word we can separete words is Pascal Cased Word and Hence can Get User Readable Name.

    private static Regex r = new Regex(
       @"(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])"
     );



Below is a C# Function to Get the Space Separated Name From Pascal Cased word.
        public static string GetNameFromPascalCaseString(string s)
        {
            if (s == null || s == "")
                return string.Empty;
            return r.Replace(s, " ");
        }

Below is another Function to Get Name Separated By Under Score '_' for each word in Pascal Cased Name.
        public static string GetNameWith_FromPascal(string s)
        {
            if (s == null || s == "")
                return string.Empty;
            return r.Replace(s, "_");
        }