Java Regular Expression Tutorial

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

About the Author

author image
mkyong
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 make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Manoj Sawant
Guest
Manoj Sawant

Thanks MkYong,
Here is Best website to check regular expressions : http://www.regexr.com/
an online tool to learn, build, & test Regular Expressions.

trackback
Some Java Links | ITAndOtherStuff
Bruno
Guest
Bruno

I can see that you love what you do…
best regards!

Arpit Panchal
Guest
Arpit Panchal

hi I need your help. actualy i need regular expression for “A-1000000”. i tried some but failed. so can you help me???

newbie~
Guest
newbie~

^[a-z0-9_-]{3,15}$
may i know the syntax ^ and $ used for??

Bruno
Guest
Bruno

^ means “at the beginning”
$ menas “at end”

^[expression]$ means that de expression must match since the beginning to the end…

sathy
Guest
sathy

^ # Start of the line
$ # End of the line

sampletestjava
Guest
sampletestjava

good work…..

Varaprasad
Guest
Varaprasad

Can you provide sample for regular expression in a file content?

Lincoln Baxter, III
Guest
Lincoln Baxter, III

I wrote another general regular expression tutorial for anyone who’s interested :) http://ocpsoft.org/opensource/guide-to-regular-expressions-in-java-part-1/ It’s not as specific as these examples, but it gives an overall demonstration of how to use the Regular Expression API in Java.

murkein
Guest
murkein

Is there a way I can review previous character and conditionate the next character in the string? Example: a string should begin with 0412… or 0414… or 0416… or 0426… which should be the regular expression?

Taj
Guest
Taj

good site for reg..

trackback
Technical Related Notes » Blog Archive » links for 2010-08-01

[…] Java Regular Expression Tutorials (tags: java regex) […]

trackback
Blog bookmarks 04/08/2010 « My Diigo bookmarks

[…] Java Regular Expression Tutorials | Tutorials […]