Back To Basics: Use Regular Expression Comments

יום ראשון, נובמבר 23, 2008

Regular expressions are much easier to understand if you use the following syntax and comment each component of the expression by using a number sign (#). To enable comments, you must also specify RegexOptions.IgnorePatternWhitespace, which means that non-escaped white space is ignored. Regex regex = new Regex(@"                         ^           # anchor at the start                        (?=.*\d)     # must contain at least one numeric character                        (?=.*)  # must contain one lowercase character                        (?=.*)  # must contain one uppercase character                        .{8,10}      # From 8 to 10 characters in length                        \s           # allows a space                        $            # anchor at the end",                        RegexOptions.IgnorePatternWhitespace);  Enjoy, Rotem
תגיות: , ,
אין תגובות