Username Regular Expression Pattern
^[a-z0-9_-]{3,15}$
Description
^ # Start of the line [a-z0-9_-] # Match characters and symbols in the list, a-z, 0-9, underscore, hyphen {3,15} # Length at least 3 characters and maximum length of 15 $ # End of the line
Whole combination is means, 3 to 15 characters with any lower case character, digit or special symbol “_-” only. This is common username pattern that’s widely use in different websites.
Password Regular Expression Pattern
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})
Description
( # Start of group (?=.*\d) # must contains one digit from 0-9 (?=.*[a-z]) # must contains one lowercase characters (?=.*[A-Z]) # must contains one uppercase characters (?=.*[@#$%]) # must contains one special symbols in the list "@#$%" . # match anything with previous condition checking {6,20} # length at least 6 characters and maximum of 20 ) # End of group
?= – means apply the assertion condition, meaningless by itself, always work with other combination
Whole combination is means, 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter and one special symbol (“@#$%”). This regular expression pattern is very useful to implement a strong and complex password.
P.S The grouping formula order is doesn’t matter.
Email Regular Expression Pattern
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)* @[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;
Description
^ #start of the line [_A-Za-z0-9-\\+]+ # must start with string in the bracket [ ], must contains one or more (+) ( # start of group #1 \\.[_A-Za-z0-9-]+ # follow by a dot "." and string in the bracket [ ], must contains one or more (+) )* # end of group #1, this group is optional (*) @ # must contains a "@" symbol [A-Za-z0-9-]+ # follow by string in the bracket [ ], must contains one or more (+) ( # start of group #2 - first level TLD checking \\.[A-Za-z0-9]+ # follow by a dot "." and string in the bracket [ ], must contains one or more (+) )* # end of group #2, this group is optional (*) ( # start of group #3 - second level TLD checking \\.[A-Za-z]{2,} # follow by a dot "." and string in the bracket [ ], with minimum length of 2 ) # end of group #3 $ #end of the line
The combination means, email address must start with “_A-Za-z0-9-\\+” , optional follow by “.[_A-Za-z0-9-]“, and end with a “@” symbol. The email’s domain name must start with “A-Za-z0-9-“, follow by first level Tld (.com, .net) “.[A-Za-z0-9]” and optional follow by a second level Tld (.com.au, .com.my) “\\.[A-Za-z]{2,}”, where second level Tld must start with a dot “.” and length must equal or more than 2 characters.
Image File Extension Regular Expression Pattern
([^\s]+(\.(?i)(jpg|png|gif|bmp))$)
Description
( #Start of the group #1 [^\s]+ # must contains one or more anything (except white space) ( # start of the group #2 \. # follow by a dot "." (?i) # ignore the case sensive checking for the following characters ( # start of the group #3 jpg # contains characters "jpg" | # ..or png # contains characters "png" | # ..or gif # contains characters "gif" | # ..or bmp # contains characters "bmp" ) # end of the group #3 ) # end of the group #2 $ # end of the string ) #end of the group #1
Whole combination is means, must have 1 or more strings (but not white space), follow by dot “.” and string end in “jpg” or “png” or “gif” or “bmp” , and the file extensive is case-insensitive.
This regular expression pattern is widely use in for different file extensive checking. You can just change the end combination(jpg|png|gif|bmp) to come out different file extension checking that suit your need.
IP Address Regular Expression Pattern
^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\. ([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$
Description
^ #start of the line ( # start of group #1 [01]?\\d\\d? # Can be one or two digits. If three digits appear, it must start either 0 or 1 # e.g ([0-9], [0-9][0-9],[0-1][0-9][0-9]) | # ...or 2[0-4]\\d # start with 2, follow by 0-4 and end with any digit (2[0-4][0-9]) | # ...or 25[0-5] # start with 2, follow by 5 and ends with 0-5 (25[0-5]) ) # end of group #2 \. # follow by a dot "." .... # repeat with 3 times (3x) $ #end of the line
Whole combination means, digit from 0 to 255 and follow by a dot “.”, repeat 4 time and ending with no dot “.” Valid IP address format is “0-255.0-255.0-255.0-255″.
Time in 12-Hour Format Regular Expression Pattern
(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)
Description
( #start of group #1 1[012] # start with 10, 11, 12 | # or [1-9] # start with 1,2,...9 ) #end of group #1 : # follow by a semi colon (:) [0-5][0-9] # follw by 0..5 and 0..9, which means 00 to 59 (\\s)? # follow by a white space (optional) (?i) # next checking is case insensitive (am|pm) # follow by am or pm
The 12-hour clock format is start from 0-12, then a semi colon (:) and follow by 00-59 , and end with am or pm.
Time in 24-Hour Format Regular Expression Pattern
([01]?[0-9]|2[0-3]):[0-5][0-9]
Description
( #start of group #1 [01]?[0-9] # start with 0-9,1-9,00-09,10-19 | # or 2[0-3] # start with 20-23 ) #end of group #1 : # follow by a semi colon (:) [0-5][0-9] # follw by 0..5 and 0..9, which means 00 to 59
The 24-hour clock format is start from 0-23 or 00-23 then a semi colon (:) and follow by 00-59.
Date Format (dd/mm/yyyy) Regular Expression Pattern
(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)
Description
( #start of group #1 0?[1-9] # 01-09 or 1-9 | # ..or [12][0-9] # 10-19 or 20-29 | # ..or 3[01] # 30, 31 ) #end of group #1 / # follow by a "/" ( # start of group #2 0?[1-9] # 01-09 or 1-9 | # ..or 1[012] # 10,11,12 ) # end of group #2 / # follow by a "/" ( # start of group #3 (19|20)\\d\\d # 19[0-9][0-9] or 20[0-9][0-9] ) # end of group #3
The above regular expression is used to validate the date format in “dd/mm/yyyy”, you can easy customize to suit your need. However, it’s a bit hard to validate the leap year , 30 or 31 days of a month, we may need basic logic as below.
P.S Do let me know, if you know the regular expression which can check the leap and days of a month.