Useful Regular Expressions

By Mohammad Mahdi Ramezanpour at March 11, 2009 19:49
Filed Under: .NET General

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/

Comments

5/16/2009 7:27:06 PM #

Arash Motallebi

Salam , khoobi ? az inke page e shoma ro peyda kardam kheili khosh halam ,
man programing ba WPF o mikham shoroo konam , visual Studio 2005 daram , .NET framework 3.0 ro ham instal kardam , chetori mitoonam WPF o enable konam ? too MSDN gashtam amma tanha extension i ke toonestam baray e visual Studio2005 peyda konam WWF bood , lotfan rahnamaeem kon
mamnoon misham
Arash

Arash Motallebi Iran

8/9/2009 10:00:26 PM #

Admin

@Arash:
Sorry because the question you asked has no relation to the article.
Please send me your question via contact section and I'll do my best to help you out.

Admin

Comments are closed

Quote of the day