Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Java Regex – What are Regular Expressions and How to Use it?

Published on Jul 22,2019 1.1K Views
A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
58 / 72 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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 in the following sequence:

Let’s get started!

What are Regular Expressions?

A 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.

Regular Expressions - Java Regex - Edureka

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.

MethodDescription
boolean matches()Tests whether the given regular expression matches the pattern
boolean find()Used to find the next expression that matches the pattern
boolean find(int start)Searches the next expression that matches the pattern from the given start number
String group()Used to return the matched subsequence
int start()Returns the starting index of the matched subsequence
int end()Returns the ending index of the matched subsequence
int groupCount()Returns the total number of the matched subsequence

Pattern Class

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

MethodDescription
static Pattern compile(String regex)It compiles the given regex and returns the instance of the Pattern
Matcher matcher(CharSequence input)It is used to create a matcher that matches the given input with the pattern
static boolean matches(String regex, CharSequence input)It works as a combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern
String[] split(CharSequence input)Used to split the given input String around matches of a given pattern
String pattern()Helps to Return the regex pattern

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 String with 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.

Character ClassDescription
[abc]a, b, or c (simple class)
[^abc]Any character except a, b, or c (negation)
[a-zA-Z]a through z or A through Z, inclusive (range)
[a-d[m-p]]a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]]d, e, or f (intersection)
[a-z&&[^bc]]a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]]a through z, and not m through p: [a-lq-z](subtraction)

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.

RegexDescription
X?X occurs once or not at all
X+X occurs once or more times
X*X occurs zero or more times
X{n}X occurs n times only
X{n,}X occurs n or more times
X{y,z}X occurs at least y times but less than z times

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.

RegexDescription
.It can be any character (may or may not match terminator)
dRepresents any digits, short of [0-9]
DRepresents any non-digit, short for [^0-9]
sRepresents any whitespace character, short for [tnx0Bfr]
SIt can be a non-whitespace character, short for [^s]
wIt can be a word character, short for [a-zA-Z_0-9]
WRepresents any non-word character, short for [^w]
bRepresents a word boundary
BIt is a non-word boundary

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. If you wish to learn more, you can check out our other Java Blogs as well.

Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.

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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.