java regular expression tutorials

Java has comprehensive support for Regular Expression functionality through the java.util.regex package. The regular expression language is easy to learn but hard to master, the better way to learn it is through examples. In theoretical, regular expression can match almost any stuff you want, the only limitation is in your imagination.

Happy learning Java Regular Expression :)

  • Username regular expression
    Username regular expression example in Java and unit tested with TestNG.

    ^[a-z0-9_-]{3,15}$
  • Password regular expression
    Password regular expression example in Java and unit tested with TestNG.

    ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})
  • Hex color code regular expression
    Hex color code regular expression example in Java and unit tested with TestNG.

    ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
  • E-mail address regular expression
    E-mail address regular expression example in Java and unit tested with TestNG.

    ^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@
    [A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$
  • Image file extension regular expression
    Image file extension regular expression example in Java and unit tested with TestNG.

    ([^\s]+(\.(?i)(jpg|png|gif|bmp))$)
  • IP Address regular expression
    IP Address regular expression example in Java and unit tested with TestNG.

    ^([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])$
  • Time in 12 Hours format regular expression
    Time in 12 Hours format regular expression example in Java and unit tested with TestNG.

    (1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)
  • Time in 24 Hours format regular expression
    Time in 24 Hours format regular expression example in Java and unit tested with TestNG.

    ([01]?[0-9]|2[0-3]):[0-5][0-9]
  • Date regular expression
    Date regular expression example in Java and unit tested with TestNG.

    (0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)
  • HTML tag regular expression
    HTML tag regular expression example in Java and unit tested with TestNG.

    <("[^"]*"|'[^']*'|[^'">])*>
  • HTML Links regular expression
    HTML links regular expression example in Java and unit tested with TestNG.

    (?i)<a([^>]+)>(.+?)</a>
    \s*(?i)href\s*=\s*(\"([^"]*\")|'[^']*'|([^'">\s]+));

Reference

Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts