Patterns and Practice - Using Regex in ASP.NET Input

by Erik Lane 24. July 2005 12:37

I've found Regex to be very powerful and useful.  I'm still green when it comes to using them but they are at the front of my mind now when it comes to validating and/or finding patterns in text.  I know there are some useful sites like http://www.regexlib.com but hopefully this short article from the Patterns and Practice Team will provide some help too.  This post is also a place for me to find it later.

They have a short table of some of the most common expressions like URL/Strong Passwords/E-Mails.  One thing that was new to me was using comments to help explain an expression.  I like this!

Use Regular Expression Comments

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
                       (?=.*[a-z])  # must contain one lowercase character
                       (?=.*[A-Z])  # must contain one uppercase character
                       .{8,10}      # From 8 to 10 characters in length
                       \s           # allows a space 
                       $            # anchor at the end", 
                       RegexOptions.IgnorePatternWhitespace);

 

Tags:
Comments are closed