Java Regular Expression Tutorial
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]+));
hi I need your help. actualy i need regular expression for “A-1000000”. i tried some but failed. so can you help me???
Thanks MkYong,
Here is Best website to check regular expressions : http://www.regexr.com/
an online tool to learn, build, & test Regular Expressions.
I can see that you love what you do…
best regards!
^[a-z0-9_-]{3,15}$
may i know the syntax ^ and $ used for??
^ # Start of the line
$ # End of the line
^ means “at the beginning”
$ menas “at end”
^[expression]$ means that de expression must match since the beginning to the end…
good work…..
Can you provide sample for regular expression in a file content?
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.
Thanks for your good guide on Java regex, and also your prettyfaces product, I’m using it for jsf2 development.
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?
good site for reg..