Sabuj Kundu 23rd Oct 2013

10698998-aspnet-programmerAccording to Wikipedia, “In computing, a regular expression (abbreviated regex or regexp) is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.”

Some .Net coders find it difficult to write expression for specific implementation. Here is the compilation of some regular expressions that will help you while programming for any project. Use this as a quick reference if you like.

 

PatternDescription
(\w+)Match one or more word characters. This is the first capturing group.
\s+Match one or more white-space characters.
(car)Match the literal string “car”. This is the second capturing group.
^[a-zA-Z”-‘\s]{1,40}$Validates a name. Allows up to 40 uppercase and lowercase characters and a few special characters that are common to some names.
[0-9]matches single-digit numbers 0 to 9.
[1-9][0-9]matches double-digit numbers 10 to 99.
1[0-9][0-9]takes care of 100 to 199.
2[0-4][0-9]matches 200 through 249.
[-+]?[0-9]*\.?[0-9]*This defines a floating point number as an optional sign, followed by an optional series of digits (integer part), followed by an optional dot, followed by another optional series of digits (fraction part).
[-+]?([0-9]*\.[0-9]+|[0-9]+)This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or that is followed by one or more digits (an integer).
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\bmatches any email address.
^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$However, then there’s another trade-off. This regex will match john@mail.office.
\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\bwill match john@server.department.company.com but not john@aol…com.
^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$matches a date in yyyy-mm-dd format from between 1900-01-01 and 2099-12-31, with a choice of four separators.
^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$will match 1999-01-01 but not 1999/01-01.
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$match a date in mm/dd/yyyy format
^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$For dd-mm-yyyy format
^4[0-9]{12}(?:[0-9]{3})?$All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
^5[1-5][0-9]{14}$All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
^3[47][0-9]{13}$American Express card numbers start with 34 or 37 and have 15 digits.
^3(?:0[0-5]|[68][0-9])[0-9]{11}$Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits.
^6(?:011|5[0-9]{2})[0-9]{12}$Discover card numbers begin with 6011 or 65. All have 16 digits.
^(?:2131|1800|35\d{3})\d{11}$JCB cards beginning with 2131 or 1800 have 15 digits. JCB cards beginning with 35 have 16 digits.
^.*\b(one|two|three)\b.*$matches a complete line of text that contains any of the words “one”, “two” or “three”.
^(?=.*?\bone\b)(?=.*?\btwo\b)(?=.*?\bthree\b).*$matches a complete line of text that contains all of the words “one”, “two” and “three”.
^\d{3}-\d{2}-\d{4}$Validates the format, type, and length of the supplied input field. 111-11-1111
^(?:\+?88)?01[15-9]\d{8}$Validates a Bangladeshi phone number.
+8801*********
^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$Validates a U.S. phone number. (425) 555-0123 425-555-0123
425 555 0123
1-425-555-0123
^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\’\/\\\+&%\$#_]*)?$Validates a URL http://www.microsoft.com

mozahid– A Guest Blog by Abu Md Al Mozahidi. Mozahid is graduate Electrical Engineer. After completion he joined Bangladesh Civil Service in 2006. Currently he is working in Bangladesh Betar as Assistant Radio Engineer. He is also enthusiast in computer programming. He is freelance .NET developer. He is skilled at HTML, CSS, C#, ASP.NET, SQL Server 2008.