# 
        Regular Expression in Java
    
This tutorial explains to you how to use Regular Expression in Java. We have an example for you as well.
The Regular Expression (RegEx) is a sequence of characters that specify a pattern used for searching or manipulating strings. The RegEx are defined in java.util.regex package.
We can group multiple characters as a unit by parentheses. Each group in a regular expression has a group number, which starts at 1.
Please take a look at the following examples and read carefully the comments. The code is self-explanatory.
This example is created from a simple Spring Boot application created with Spring Initializr. I am using Maven, Java 17, Spring Boot 3.1.0.
Starting from the base application downloaded from Spring Initializr, I updated the main class as below:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SpringBootApplication
public class DemoApplication {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// Create the application context and start Spring Boot
		ApplicationContext appContext = SpringApplication.run(DemoApplication.class, args);
		// Defines a pattern (to be used in a search)
		Pattern p = Pattern.compile("aaa|Bbb", Pattern.CASE_INSENSITIVE);
		// Search for the pattern and keep some information regarding the search
		Matcher m = p.matcher("aaa fsasfa afasfas afasfa afasfsf bbbb aaa.");
		// There are no groups defined in RegEx
		System.out.println("Number of groups: " + m.groupCount());
		// For each pattern found in the string
		while (m.find()) {
			// m.group() shows the pattern found
			System.out.println(String.format("Pattern found: %s", m.group()));
			// m.start(), m.end() shows the start and the end position in the string for that finding
			System.out.println(String.format("Start and end position: %s %s \n", m.start(), m.end()));
		}
		String strWithEmails = " 39000 7900 8000 8900 8700";
		// Any number 4 character length which starts with 3,4 or 8 and has the last 3 characters 900.
		var regEx = "[348]900";
		// Remove all the numbers defined in regEx variable
		var newString1 = strWithEmails.replaceAll(regEx,"");
		System.out.println("newString1="+newString1);
		// Remove the pattern when it is found for the first time
		var newString2 = strWithEmails.replaceFirst(regEx,"");
		System.out.println("newString2="+newString2);
		// Split the initial string using a RegEx expression
		var stringArray = strWithEmails.split(regEx);
		System.out.print("values= ");
		for (String val1 : stringArray) {
			System.out.print(val1+",");
		}
		System.out.println("");
		// Test if the whole string follows the RegEx pattern
		var result = strWithEmails.matches(regEx);
		if (result) {
			System.out.println("The whole string follow the RegEx pattern");
		} else {
			System.out.println("The whole string DOES NOT follow the RegEx pattern");
		}
	}
}When I run this code I get the following log:
Number of groups: 0
Pattern found: aaa
Start and end position: 0 3
Pattern found: bbb
Start and end position: 34 37
Pattern found: aaa
Start and end position: 39 42
newString1= 0 7900 8000  8700
newString2= 0 7900 8000 8900 8700
values=  ,0 7900 8000 , 8700,
The whole string DOES NOT follow the RegEx pattern
Process finished with exit code 0The java.util.regex package provides following classes for working with regular expressions:
- PatternClass - Defines a pattern (to be used in a search)
- MatcherClass - Used to search for the pattern
We can see some basic functionalities of RegEx in the following Java code as well:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SpringBootApplication
public class Demo1Application {
	public static void main(String[] args) throws InterruptedException {
		SpringApplication.run(Demo1Application.class, args);
		String str1 = "a";
		String str2 = "aC";
		String str3 = "aCaaaCff";
		String str5 = "elephant10@hotmail.com";
		String str6 = "elephant10@hotmailcom";
		System.out.println("01. "+str1.matches("[abc]"));
		System.out.println("02. "+str2.matches("[.][C]"));
		System.out.println("03. "+str2.matches("[.C]"));
		System.out.println("04. "+str3.matches("aC[\\w]+"));
		String regex = "(\\w+)@([a-z]+)\\.([a-z]{3})";
		// Create a compiled representation of the regular expression
		Pattern p = Pattern.compile(regex);
		// Create a "search engine" to search in a string for the pre-defined pattern
		Matcher m1 = p.matcher(str5);
		Matcher m2 = p.matcher(str6);
		System.out.println("05. "+m1.find());
		System.out.println("06. "+m2.find());
		//Show a group from RegEx
		System.out.println("07. "+m1.group(2));
	}
}When we run this code we receive the following result:
01. true
02. false
03. false
04. true
05. true
06. false
07. hotmailMore information regarding greedy, possessive, and reluctant quantifiers you have here.
 
                                