Java Regex – Matcher

What is a Regex in Java?

A


import java.util.regex.*;

Java Regex – Matcher

The Java Matcher Class provides pattern matching with regular expressions.

This line of code imports all classes available in the regex package. If you want to import only the

Matcher

class you can import it using the following line of code.


import java.util.regex.Matcher;

The java regex package has three classes:

  1. Pattern Class: To define the pattern that needs to be searched.

  2. Matcher Class: To match the pattern in the text and find it.

  3. PatternSyntaxException Class: To indicate any syntax errors in the regular expression.

The working of it is quite simple. First, you create a

Pattern

object from the regular expression to define a pattern. Then a

Matcher

object is created using the

Pattern

object. The

Matcher

class has many methods. The most important of these is the

matches()

method which returns true if the regular expression matches the text, else returns false. Java

Matcher

Class has many other useful methods for replacing text in the input strings that perform more complex functions than the

replace()

methods in java. There are only two

replace()

methods in java, whereas the

Matcher

class provides multiple functions for this task.

Java Matcher Class Methods

Some of the Java

Matcher

Class methods are described below.

  • boolean matches() — returns true if the regular expression matches the pattern.

  • boolean find() — finds the next regular expression that matches the pattern.

  • boolean find(int start) — finds the next regular expression that matches the pattern from the given start.

  • int start() — returns the starting index of the match.

  • int end() — returns the ending index of the returned match.

  • int groupCount() — returns the total number of groups in the match.

  • String replaceAll(String replacement) — replaces every subsequence of the match with the given replacement.

  • String replaceFirst(String replacement) — replaces the first subsequence of the match with the given replacement.

Java Matcher Example

There are many useful methods for the

Matcher

class, however, we will further be looking at the specific example(s) of the

matches()

method.

Using the matches() method

The following demonstrates a Java

Matcher

Example for

matches()

method.


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {

	public static void main(String[] args) {

		String regexPattern = "(.*)apple(.*)"; // this regex means that any char sequence can precede or succeed "apple" 
		String firstCheck = "Apples";
		String secondCheck = "Apple";
		String thirdCheck = "apple";
		String fourthCheck = "An apple a day keeps the doctor away.";
		String fifthCheck = "green apple, yellow apple, red apple, i love all kinds of apples.";

		
		Pattern pattern = Pattern.compile(regexPattern);
		Matcher matcher1 = pattern.matcher(firstCheck);

		System.out.println("The Pattern  is: " + pattern);
		System.out.println("Text to check is: " + firstCheck);
		System.out.println("Output for matches(): " + matcher1.matches() + "\n");

		Matcher matcher2 = pattern.matcher(secondCheck);

		System.out.println("The Pattern  is: " + pattern);
		System.out.println("Text to check is: " + secondCheck);
		System.out.println("Output for matches(): " + matcher2.matches() + "\n");

		Matcher matcher3 = pattern.matcher(thirdCheck);

		System.out.println("The Pattern  is: " + pattern);
		System.out.println("Text to check is: " + thirdCheck);
		System.out.println("Output for matches(): " + matcher3.matches() + "\n");
		
		Matcher matcher4 = pattern.matcher(thirdCheck);

		System.out.println("The Pattern  is: " + pattern);
		System.out.println("Text to check is: " + fourthCheck);
		System.out.println("Output for matches(): " + matcher4.matches() + "\n");
		
		Matcher matcher5 = pattern.matcher(fifthCheck);

		System.out.println("The Pattern  is: " + pattern);
		System.out.println("Text to check is: " + fifthCheck);
		System.out.println("Output for matches(): " + matcher5.matches() + "\n");
	}
}

Output

The Pattern is: (.*)apple(.*)
Text to check is: Apples
Output for matches(): false

The Pattern is: (.*)apple(.*)
Text to check is: Apple
Output for matches(): false

The Pattern is: (.*)apple(.*)
Text to check is: apple
Output for matches(): true

The Pattern is: (.*)apple(.*)
Text to check is: An apple a day keeps the doctor away.
Output for matches(): true

The Pattern is: (.*)apple(.*)
Text to check is: green apple, yellow apple, red apple, i love all kinds of apples.
Output for matches(): true

Java Regex - Matcher - 1

Conclusion

By the end of this post, we hope you have got yourself familiarized with the Java

Matcher

Class in Java. You have learned how to use it in your code. You have also learned how to call the

matches()

method of

Matcher

Class. You can try other methods of the

Matcher

Class on your own to understand it in more depth. Keep practicing for a deeper command of the concept. Till then, keep growing and keep shining!regular expression or regex is a sequence of characters that form a pattern. When you are searching for any data you can use this specialized pattern to find matching strings. It can be as simple as a single character or it can be a more complex pattern. In Java, you have a java regex package that has classes that provide the facility of using regular expressions for pattern searching and manipulation. This package is imported into your code in the following way.This line of code imports all classes available in the regex package. If you want to import only theclass you can import it using the following line of code.The java regex package has three classes:The working of it is quite simple. First, you create aobject from the regular expression to define a pattern. Then aobject is created using theobject. Theclass has many methods. The most important of these is themethod which returns true if the regular expression matches the text, else returns false. JavaClass has many other useful methods for replacing text in the input strings that perform more complex functions than themethods in java. There are only twomethods in java, whereas theclass provides multiple functions for this task.Some of the JavaClass methods are described below.There are many useful methods for theclass, however, we will further be looking at the specific example(s) of themethod.The following demonstrates a JavaExample formethod.OutputBy the end of this post, we hope you have got yourself familiarized with the JavaClass in Java. You have learned how to use it in your code. You have also learned how to call themethod ofClass. You can try other methods of theClass on your own to understand it in more depth. Keep practicing for a deeper command of the concept. Till then, keep growing and keep shining!