Regular Expression in java

The Regular Expressions in java is also known as Regex in short. Regex API is used for defining or matching String patterns that can be used for searching, manipulating and editing a string in Java. Email, as well as Phone validation and passwords, are few areas of strings where Regex is widely used to define the constraints.

In Java, Regular Expressions are provided under java.util.regex package. This consists of 1 interface and 3 classes which are given below in detail.

The java.util.regex package mainly contains the following three classes:

  • The regex. Pattern class is used for defining patterns from strings
  • The util.regex.Matcher class is used for performing match operations on text using patterns.
  • And the last one is PatternSyntaxException which is used for an indication syntax error in a regular expression pattern

Now we discuss each individual class in detail so you can easily catch the points.

  • util.regex.Pattern class is used to define various types of patters, providing no public constructors. This can be generated by invoking the compile() method which accepts a regular expression as the first argument, thus returns a pattern after execution.


import java.util.regex.Pattern;

class CodeDecDemo


public static void main(String args[])


//This following line prints "true" because the whole

// text "codedec" matches pattern "codedec*dec"

System.out.println (Pattern.matches("codedec*dec",


// This following line prints "false" because the whole

// text "codefor" doesn't match pattern "c*codes*"

System.out.println (Pattern.matches("c*codes*",




java.util.regex.Matcher class is used for match operations for an input string in java, thus interpreting the previously explained patterns.

This too defines no public constructors implemented by invoking a matcher() on any pattern object.


import java.util.regex.Matcher;

import java.util.regex.Pattern;

class CodeDecDemo


public static void main(String args[])


// Create a pattern to be searched

Pattern pattern = Pattern.compile("co*");

// Search above pattern in ""

Matcher m = pattern.matcher("");

// Print the result if matched

// in text

while (m.find())

System.out.println("Pattern found");



Validate phone number using Regex in java

List phoneNumbers = new ArrayList(); // creating array of phone numbers

phoneNumbers.add("+1 1234567890120");

phoneNumbers.add("+12 123456780");

phoneNumbers.add("+123 123450");

String regex = "^\\+(?:[0-9] ?){6,14}[0-9]$"; // Use the special characters for checking phone number validation

Pattern pattern = Pattern.compile(regex);

for(String email: phoneNumbers)


Matcher matcher = pattern.matcher(email);

System.out.println(email +" : "+ matcher.matches());



+1 1234567890120: true

+12 123456780: true

+123 123450: true

 PatternSyntaxException class is used for indicating a syntax error in a regular expression pattern and is an unchecked exception.

Here is some Important Observations/Facts :

  1. We create some patterns of an object by calling Pattern.compile(), there is no constructor. compile() is a static method in Pattern class.
  2. Like above, we create a Matcher object using a matcher() on objects of Pattern class.
  3. matches() is also a static method that is used to check if given text as a whole matches pattern or not.
  4. find() is used to find multiple occurrences of patterns in the text.
  5. We can split a text based on a delimiter pattern
    using split()