Thursday, January 27, 2011

Most frequently used Regular Expressions in Java.


Username :
------------


 ^[a-z0-9_-]{3,15}$

Notes :

 ^             Start of the line
[a-z0-9_-]   supported chars 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.


Password :
----------

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})

Notes :

  (?=.*\d)  must contains one digit from 0-9
  (?=.*[a-z])  must contains one lowercase characters
  (?=.*[@#$%])  must contains one special symbols in the list "@#$%"

Let us see more





E-mail
--------

^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

Notes :

[_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 [ ],
(         start of group #2 - first level TLD checking
\\.[A-Za-z0-9]+    follow by a dot "." and string in the bracket [ ]
      )*                  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
$

Image

([^\s]+(\.(?i)(jpg|gif|bmp))$)


 [^\s]+   must contains one or more anything (except white space)
       (    start of the group #2
         \. follow by a dot "."
         (?i) ignore the case sensitive checking
             ( start of the group #3
              jpg contains characters "jpg"
              | ..or
              | ..or
              bmp contains characters "bmp"
             ) end of the group #3
       )    end of the group #2
  $        end of the string
)



IP address
----------

^([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])$


   [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 end with 0-5 (25[0-5])
 )                  end of group #2
  \.                follow by a dot "."

Time Format Regular Expression Pattern

Time in 12-Hour Format Regular Expression Pattern

(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)

 1[012]      start with 10, 11, 12
 :     follow by a  colon (:)
  [0-5][0-9]   follow 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

Time in 24-Hour Format Regular Expression Pattern

([01]?[0-9]|2[0-3]):[0-5][0-9]

8. 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)

 Key Notes :

 0?[1-9] 01-09 or 1-9

 [12][0-9] 10-19 or 20-29
  3[01] 30, 31
(19|20)\\d\\d 19[0-9][0-9] or 20[0-9][0-9]


9. HTML tag Regular Expression Pattern

<("[^"]*"|'[^']*'|[^'">])*>

<        start with opening tag "<"
    "[^"]*"    only two double quotes are allow - "string"
    '[^']*'    only two single quotes are allow - 'string'
    [^'">] cant contains one single quotes, double quotes and ">"
 *        0 or more


10. HTML links Regular Expression Pattern
HTML A tag Regular Expression Pattern

(?i)<a([^>]+)>(.+?)</a>

No comments:

Post a Comment

subversion video