14 Important Standard Regular Expressions


Regular Expressions are mainly used for pattern matching i.e. mostly UI level validations like E-mail-Id, password, number-field validations, etc., and are language independent, concise and really interesting (when they are tough to form).


I am not going to tell you how to form or use regular expression (also known as regex) as there are many really good websites available out there like -
http://www.regular-expressions.info
http://regexlib.com/
http://coding.smashingmagazine.com/2009/06/01/essential-guide-to-regular-expressions-tools-tutorials-and-resources/


How to use :-
In Javascript, to search a pattern in a string variable, use -
var stringToMatch = "this is my pattern";
if(stringToMatch.search(/pattern/i)!= -1)
    alert("Pattern matched.");
else
    alert("Pattern not matched.");


OR in java, make use of java.util.regex.Pattern and java.util.regex.Matcher OR simply String class's methods matches(), replaceAll() and split().
Forming Regular-expressions for a particular condition is both challenging and a lot of fun.


Here are few standard regular-expressions which I had written over the period.


1) Asset ID (pattern e.g. 01HW211212) - Asset ID are most commonly used sequences to identify any asset, to categories, to maintain their database. As pattern can be different, so modify accordingly.
//e.g. Where pattern of Asset-ID is - (two digits any from 0-9)(two letters any from A-Z or a-z)(6 digits any combination from 0-9)
//e.g. 07gw456754, 98ju321234
Regular Expression - ^(\d{2}[A-Za-z]{2}\d{6})$


2) Mac Address -
//Mac Address of PC like 00-00-00-00-00-00 to ff-ff-ff-ff-ff-ff
Regular Expression - ^(([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2})$


3) IP Address -


    a) IPv4 -
        i) Public IP -
//Public IP Addresses are all in range 0.0.0.0 to 255.255.255.255 except private ip addresses.
Regular Expression - ^(([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])$


         ii) Private IP Address (Block 1) -
//For Range - 10.0.0.0 to 10.255.255.255
Regular Expression - ^(10\.(([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){2}([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))$


        iii) Private IP Addresses (Block 2) -
For Range - 192.168.0.0 to 192.168.255.255
Regular Expression - ^(192\.168\.([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))$


        iv) Private IP Addresses (Block 3) -


//For Range - 172.16.0.0 to 192.31.255.255
Regular Expression - ^(172\.(1[6-9]|2[0-9]|3[0-1])\.([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))$


        v) Private IP Addresses (Block 4) -
//169.254.0.0 through 169.254.255.255 (APIPA only)
Regular Expression - ^(169\.254\.([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([0,1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))$


4) Simple TextField/NumberField Validator Regex - Different datatype ranges for Java datatypes
    a) For Integer Datatype Range (-128 to +127) -
Regular Expression - ^([-+]?(\d{1,2}|1[0-1]\d|12[0-7])|-128)$


   b) For Short Datatype Range (-32,768 to 32,767)-
Regular Expression - ^([-+]?([0-2]?\d{1,4}|3[0-2][0-6]\d{2}|327[0-5]\d|3276[0-7])|-32768)$


   c) Allow only numeric values From 0.00 to 100.00 -
Regular Expression - ^(100.[0]+|[0]+.[0]+|\d{1,2}|\.\d{1,2}|\d{1,2}\.\d{1,2})$


   d) Real number validations in TextField - 
        Max 15 digits before decimal and 6 digits after decimal point are allowed
Regular Expression - ^(\d{0,15}|\.\d{1,6}|\d{0,15}\.\d{1,6})$


5) Email Regex Validator -
Regular Expression - ^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$


6) TimeStamp regex -
    a) 12 hrs Format - (11:59:59:999 PM / 02:59 am / 7:34 pm)
Regular Expression - ^(([0]?\d|1[0-2]):([0-5]?[0-9])(:([0-5]\d):(\d{3}))?[ ]*((am|AM)|(pm|PM))?)$


   b) 24 hrs Format - (24:00 or 23:59:00:999) -
Regular Expression - ^((([0-1]\d|2[0-3]):([0-5]?[0-9])(:([0-5]\d):(\d{3}))?)|24:00:00:000)$


7) Date format regex - (for format - mm/dd/yyyy) (Max Year limit 3600) -
Regular Expression - ^(([0]?[1-9]|1[0-2])/([0]?[1-9]|[1-2]\d|3[0-1])/([0-2]?\d{1,3}|3[0-5]\d{2}|3600))$


8) Image File names regex (For Windows) (allowed filetypes bmp, jpg, jpeg, png, gif, tif, tiff, giff) -
Regular Expression - ^([^\\/*":?<>|]{1,244}\.(bmp|jpg|jpeg|png|gif|giff|tif|tiff))$


9) Username Validator Regex -
//Always starts with alphabet and not a number or special character and
//can contain charcters as 0-9, a-z, A-Z and @, -,.,_
Regular Expression - ^(([a-zA-Z]+)([a-zA-Z0-9@.-_]+))$


10)Username Validator Regex - (Alphanumeric Combination)
//Always starts with alphabet and not a number, can contain 0-9 digits and a-z, A-Z letters.
Regular Expression - ^(([a-zA-Z]+)([a-zA-Z0-9]+))$


11) Color Hex Code Regex -      
//e.g. #F0F8FF - Pattern - #(six occurrences of 0 to 9 digits or a-f or A-F characters)
Regular Expression - ^(#[a-fA-F0-9]{6})$


12) Telephone Number Regex -
//US Telephone numbers e.g. +1-234-543-4533
US - Regular Expression - ^([+]?[0-9]{1,2}-[0-9]{3}-[0-9]{3}-[0-9]{4})$

//Indian Telephone numbers e.g. +91-96783-64533 
INDIA - Regular Expression - ^([+]?91-[0-9]{5}-[0-9]{5})$

//Generally Telephone numbers in the world are of format e.g. +76-967-654-4533 
World - Regular Expression -  ^([+]?[0-9]{1,2}-[0-9]{3}-[0-9]{3}-[0-9]{4})$


13) SSN Number Regex Validator -
Regular Expression - ^([0-9]{3}-[0-9]{2}-[0-9]{4})$


14) Validate Postal Codes  -
//Indian Postal / PIN Codes - e.g. 411033, 100342, etc.
Indian Postal Codes - Regular Expression - ^([0-9]{6})$

//US ZIP codes e.g 34212-2234
US - Regular Expression - ^(\d{5}(-\d{4})?)$

//Germany's Postal Codes - e.g. 44232
GERMANY - Regular Expression - ^([0-9]{5})$

Note - To validate regex you can use - online regex validators or mozilla addon- e.g. websites like - 
http://public.kvalley.com/regex/regex.asp


Here is one link which I found is also helpful if you are looking for some standard regex expressions.
http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/


Enjoy. :-)

Comments

Popular posts from this blog

How to install / Configure Mantis For Apache-PHP-PostgreSQL combination

Modified ExtJS LOVCombo (List of Values) ux plugin - with select all, deselect all, text filter, bubble up and bubble down of selection

TriggerField with two triggers, TwinTriggerField with tooltips