Codementor Events

Java Regex Tutorial | Regular Expressions in Java

Published Jul 23, 2019
Java Regex Tutorial | Regular Expressions in Java

Data extraction or validation is an important aspect of every programming language. One of the most popular ways for data validation is by using regular expressions. Java uses these regular expressions to describe a pattern of characters. This article on Java Regex will list out the various methods of using expressions.

Let’s get started!

What are Regular Expressions?

Regular Expression is a sequence of characters that constructs a search pattern. When you search for data in a text, you can use this search pattern to describe what you are looking for.

A regular expression can be a single character or a more complicated pattern. It can be used for any type of text search and text replace operations. A Regex pattern consist of simple characters, such as /abc/, or a combination of simple and special characters, such as /ab*c/ or  /example(d+).d*/.

What is Java Regex?

The  Java Regex is an API which is used to define a pattern for searching or manipulating Strings. It is widely used to define the constraint on Strings such as password and email validation.

There are different methods of using Java Regex. So let’s move ahead and have a look at the different expressions.

Matcher Class

This class is used to perform match operations on a character sequence. Below table represents various methods of Matcher class.

Capture.PNG

Pattern Class

Pattern Class is a compiled version of regular expression which is used to define the pattern for regex engine.

Capture.PNG

Now let’s take a small example to understand how to write a regular expression.

import java.util.regex.*;
  public class RegexExample{
    public static void main (String[] args){
       Pattern pattern = Pattern.compile(".xx.");
       Matcher matcher = pattern.matcher("AxxB");
       System.out.println("String matches the given Regex - +matcher.matches());
   }
}

In this case, internally it uses Pattern and Matcher Java regex classes to do the processing but obviously, it reduces the code lines. Pattern class also contains matches method that takes regex and input String as argument and returns a boolean result after matching them. So the code works fine for matching input Stringwith a Regular expression in Java. Hence the output will be true as shown below.

Output:
true

Now let’s see a few more categories of Java Regular Expressions.

Regex Character Class

Below table represents the different character class combination.

Capture.PNG

Example:

import java.util.regex.*;
  public class CharacterExample{
    public static void main(String args[]){     
      //false (not x or y or z)
      System.out.println(Pattern.matches("[xyz]", "wbcd"));
      //true (among x or y or z)
      System.out.println(Pattern.matches("[xyz]", "x"));
      //false (x and y comes more than once)
      System.out.println(Pattern.matches("[xyz]", "xxyyyyyz"));
    }
}

Regex Quantifiers

The quantifiers specify the number of occurrences of a character. Below table represents various quantifiers.

Capture.PNG

Example:

import java.util.regex.*;
   public class Example{
     public static void main(String args[]){
       System.out.println("? quantifier ....");

       //(a or y or z comes one time)
       System.out.println(Pattern.matches("[ayz]?", "a")); //output: true
       System.out.println(Pattern.matches("[ayz]?", "aaa")); 

       //(a y and z comes more than one time)
       System.out.println(Pattern.matches("[ayz]?", "ayyyyzz")); //output: false

       //(a comes more than one time)
       System.out.println(Pattern.matches("[ayz]?", "amnta")); //output: false

       //(a or y or z must come one time)
       System.out.println(Pattern.matches("[ayz]?", "ay")); //output: false 
       System.out.println("+ quantifier ....");

       //(a or y or z once or more times)
       System.out.println(Pattern.matches("[ayz]+", "a")); //output: true

       //(a comes more than one time)
       System.out.println(Pattern.matches("[ayz]+", "aaa")); //outpu: true

       //(a or y or z comes more than once)
       System.out.println(Pattern.matches([amn]+", "aayyyzz")); //output: true

       //(z and t are not matching pattern)
       System.out.println(Pattern.matches("[ayz]+", "aammta")); //output: false
       System.out.println("* quantifier ....");

       //(a or y or z may come zero or more times)
       System.out.println(Pattern.matches("[ayz]*", "ayyyza")); //output: true
    }
}

Basically, it will search for the matching quantifier and matches the search result.

Regex Metacharacters

The regular expression metacharacters work as shortcodes. Let’s have a look at the below table to understand various types of metacharacters.

Capture.PNG

Example:

import java.util.regex.*;
   public class MetacharExample{
     public static void main(String args[]){
       // d means digit
       System.out.println("metacharacters d...."); 
       //(non-digit)
       System.out.println(Pattern.matches("d", "abc"));//Output: false 

       //(digit and comes once)
       System.out.println(Pattern.matches("d", "1"));//Output: true 

       //(digit but comes more than once)
       System.out.println(Pattern.matches("d", "4443")); //Output: false

       //(digit and char)
       System.out.println(Pattern.matches("d", "323abc"));//Output: false
       //D means non-digit
       System.out.println("metacharacters D....");

       //(non-digit but comes more than once)
       System.out.println(Pattern.matches("D", "abc")); // Output: false

       //Its a Digit
       System.out.println(Pattern.matches("D", "1")); //Output: false 
       System.out.println(Pattern.matches("D", "4443")); //Output: false 

       // (digit and char)
       System.out.println(Pattern.matches("D", "323abc")); //Output: false
       //(non-digit and comes once)
       System.out.println(Pattern.matches("D", "m")); //Output: true 

       System.out.println("metacharacters D with quantifier....");
       //(non-digit and may come 0 or more times)
       System.out.println(Pattern.matches("D*", "abc")); //Output: true 

     }
}

Based on the above-mentioned conditions, it will display the output. That’s how it works. So, That was all about various types of Java Regex. With this, we come to the end of this article. I hope you found it informative.

Got a question for us? Please mention it in the comments section of this “ Java Regex” article and we will get back to you as soon as possible.

Discover and read more posts from Neha
get started
post commentsBe the first to share your opinion
Show more replies