Regular Expression Patterns
From various sources I have collected the following regular expression patterns. They helped me a lot in front-end and back-end validation.
Alpha:
Pattern: ^[a-zA-Z_ ]+$
Description: Allows only ‘A-Z’, ‘a-z’, space ( ‘ ‘ ) and underscore ( ‘_’).
Alphanumeric:
Pattern: ^[a-zA-Z0-9_ ]+$
Description: Allows only ‘A-Z’, ‘a-z’, ‘0-9’, space ( ‘ ‘ ) and underscore ( ‘_’).
Email:
Pattern: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
Numeric:
Pattern: ^\d+$
Decimal:
Pattern: ^\d+(?:\.\d{0,2})?$
Date – 1:
Pattern: ^(0[1-9]|[1,2][0-9]|3[0-1])-(0[1-9]|1[0,1,2])-\d{4}$
Description: Date format DD-MM-YYYY
Date – 2:
Pattern: ^(0[1-9]|1[0,1,2])-(0[1-9]|[1,2][0-9]|3[0-1])-\d{4}$
Description: Date format MM-DD-YYYY
US Zip Code:
Pattern: ^\d{5}$
US Telephone Number:
Pattern: ^1?[\-]?\(?\d{3}\)?[\-]?\d{3}[\-]?\d{4}$
Color Code:
Pattern: ^#{1}?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$
Any HTML Tag:
Pattern: <([^">]+(?:”[^”]+”)*[^>]+)>
5 Comments
Other Links to this Post
RSS feed for comments on this post. TrackBack URI
By Arafat Rahman, June 29, 2008 @ 2:29 pm
Thanks for the post. It covers common regular expression patterns.
Here you can find more details about regular expression
http://www.ilovejackdaniels.com/cheat-sheets/regular-expressions-cheat-sheet/
By Mohammad Sajjad Hossain, July 2, 2008 @ 11:25 pm
Thank you Arafat for providing the link. It is really helpful.
By Naseer Ahmad Mughal, September 15, 2008 @ 2:37 pm
Thanks both of you for such a nice information to share.
By Raihan, November 26, 2008 @ 11:04 am
Here you can find millions – http://www.regexlib.com
By Mohammad Sajjad Hossain, November 26, 2008 @ 11:23 am
@Thank you Naseer.
@Raihan Bhai, thanks for the link. My intension was to provide some expressions which we need time to time.