Today I’ve developed a class library which manages some text validations and named it “RegularExpressions.cs”. It was an easy-to-develop class but, some of the expressions were good. It uses the System.Text.RegularExpressions.Regex class in order to check for the validations. In this post, I’m going to name some of the most useful regular expressions, you may need in your usual developments.
Numeric:
Pattern: ^[-+]?\d*\.?\d*$
Matches: 123 | +3.14159 | -3.14159
Non-Matches: abc | 3.4.5 | $99.95
Email Address:
Pattern: ^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
Matches: asmith@mactec.com | foo12@foo.edu | bob.smith@foo.tv
Non-Matches: joe | @foo.com | a@a
URL:
Pattern: ^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$
Matched: http://www.blah.com/~joe | ftp://ftp.blah.co.uk:2828/blah%20blah.gif | https://blah.gov/blah-blah.as
Non-Matches: www.blah.com | http://www.blah"blah.com/I have spaces! | ftp://blah_underscore/[nope]
For complete list of useful regular expression you can check out: http://www.regexlib.com/
f629502c-231e-4c82-a068-69c135ef257e|0|.0