Main Tutorials

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 Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Arpit Panchal
11 years ago

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

Manoj Sawant
9 years ago

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

Bruno
10 years ago

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

newbie~
11 years ago

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

sathy
9 years ago
Reply to  newbie~

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

Bruno
10 years ago
Reply to  newbie~

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

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

sampletestjava
11 years ago

good work…..

Varaprasad
11 years ago

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

Lincoln Baxter, III
11 years ago

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
11 years ago

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
11 years ago

good site for reg..