Java Regex
Java Regex
The Java Regex or Regular Expression is an API to define pattern for searching or
manipulating strings.
It is widely used to define constraint on strings such as password and email
validation. After learning java regex tutorial, you will be able to test your own regular
expressions by the Java Regex Tester Tool.
Java Regex API provides 1 interface and 3 classes in java.util.regex package.
java.util.regex package
It provides following classes and interface for regular expressions. The Matcher and
Pattern classes are widely used in java regular expression.
1. MatchResult interface
2. Matcher class
3. Pattern class
4. PatternSyntaxException class
Matcher class
It implements MatchResult interface. It is a regex engine i.e. used to perform match
operations on a character sequence.
No. Method
1
Description
boolean
matches()
pattern.
boolean find()
boolean find(int
start)
Pattern class
It is the compiled version of a regular expression. It is used to define a pattern for the
regex engine.
No. Method
1
Description
regex)
instance of pattern.
Matcher
matcher(CharSequence input)
String[] split(CharSequence
input)
String pattern()
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
copy to clipboard
import java.util.regex.*;
public class RegexExample1{
public static void main(String args[]){
//1st way
Pattern p = Pattern.compile(".s");//. represents single character
Matcher m = p.matcher("as");
boolean b = m.matches();
//2nd way
boolean b2=Pattern.compile(".s").matcher("as").matches();
//3rd way
boolean b3 = Pattern.matches(".s", "as");
System.out.println(b+" "+b2+" "+b3);
}}
import java.util.regex.*;
public class RegexExample1{
public static void main(String arg
//1st w ay
Pattern p = Pattern.compile(".s")
Test it Now
Output
true true true
import java.util.regex.*;
class RegexExample2{
public static void main(String arg
System.out.println(Pattern.match
System.out.println(Pattern.match
Test it Now
Description
[abc]
a, b, or c (simple class)
[^abc]
[a-zA-Z]
[a-d[m-p]]
[a-z&&[def]]
d, e, or f (intersection)
[a-z&&[^bc]]
[a-z&&[^m-p]]
import java.util.regex.*;
class RegexExample3{
public static void main(String arg
System.out.println(Pattern.match
System.out.println(Pattern.match
Test it Now
Regex Quantifiers
The quantifiers specify the number of occurrences of a character.
Regex
Description
X?
X+
X*
X{n}
X{n,}
X{y,z}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
copy to clipboard
import java.util.regex.*;
class RegexExample4{
public static void main(String args[]){
System.out.println("? quantifier ....");
System.out.println(Pattern.matches("[amn]?",
e time)
System.out.println(Pattern.matches("[amn]?",
n one time)
System.out.println(Pattern.matches("[amn]?",
omes more than one time)
System.out.println(Pattern.matches("[amn]?",
han one time)
System.out.println(Pattern.matches("[amn]?",
ome one time)
"a"));//true (a or m or n comes on
"aaa"));//false (a comes more tha
"aammmnn"));//false (a m and n c
"aazzta"));//false (a comes more t
"am"));//false (a or m or n must c
Test it Now
Regex Metacharacters
The regular expression metacharacters work as a short codes.
Regex
Description
\d
\D
\s
\S
\w
\W
\b
A word boundary
\B
copy to clipboard
import java.util.regex.*;
class RegexExample5{
public static void main(String args[]){
System.out.println("metacharacters d....");\\d means digit
Test it Now
copy to clipboard
/*Create a regular expression that accepts alpha numeric characters only. Its
length must be 6 characters long only.*/
import java.util.regex.*;
class RegexExample6{
public static void main(String args[]){
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}",
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}",
re than 6 char)
9.
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}",
10.
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}",
t matched)
11.
}}
"arun32"));//true
"kkvarun32"));//false (mo
"JA2Uk2"));//true
"arun$2"));//false ($ is no
Test it Now
copy to clipboard
/*Create a regular expression that accepts 10 digit numeric characters
starting with 7, 8 or 9 only.*/
import java.util.regex.*;
class RegexExample7{
public static void main(String args[]){
System.out.println("by character classes and quantifiers ...");
System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));//true
System.out.println(Pattern.matches("[789][0-9]{9}", "9953038949"));//true
System.out.println(Pattern.matches("[789][0-9]{9}", "99530389490"));//false (1
1 characters)
12.
System.out.println(Pattern.matches("[789][0-9]{9}", "6953038949"));//false (st
arts from 6)
13.
System.out.println(Pattern.matches("[789][0-9]{9}", "8853038949"));//true
14.
15.
System.out.println("by metacharacters ...");
16.
System.out.println(Pattern.matches("[789]{1}\\d{9}", "8853038949"));//true
17.
System.out.println(Pattern.matches("[789]{1}\\d{9}", "3853038949"));//false (s
tarts from 3)
18.
19.
}}