Introduction
In this article, we will learn about Java Regex and how to use Regex with examples in Java. Java regex is also known as Java Regular Expression.
What is Regex in Java?
Regular expressions or Java Regex is an API built to define string patterns that can be used to read, alter, and delete data. For pattern matching with the regular expressions, Java offers java.util.regex bundle.
In other words, a regular expression is a special sequence of characters that helps us match or find other strings using a special syntax held in a pattern that is used to search, edit, or manipulate text and data.
java.util.regex package
This provides regular expressions with three classes and a single interface. The classes Matcher and Pattern are usually used in standard Java language.
The complete program of the regex package is listed below.
import java.util.regex.Pattern;
public class RegexPackageExample {
public static void main(String args[]) {
System.out.println(Pattern.matches(".y", "toy"));
System.out.println(Pattern.matches("s..", "sam"));
System.out.println(Pattern.matches(".a", "mia"));
}
}
The above program generates the following output.

PatternSyntaxException class
PatternSyntaxException is an unresolved exception object which means a syntax error in a normal speaking pattern.
The complete program of Showing the PatternSyntaxException class example is listed below.
import java.util.regex.Pattern;
public class PatternSyntaxExceptionExample {
public static void main(String... args) {
String regex = "["; // invalid regex
Pattern pattern = Pattern.compile(regex);
}
}
The above program generates the following output.

Note. In the above example program, we use the invalid syntax of regex. So, when we run the program, it generates the PatternSyntaxException: Unclosed character class near index 0;
java.util.regex.Matcher;
A Matcher entity is a motor that interprets the template against an input string and executes operations of play. Matcher doesn't describe any public builders as the class Template. By calling the matcher method) (You get a Matcher object on a Pattern object.
Methods of Matcher class
public boolean matches()
The matches method is used to check whether the pattern string matches with the matcher string or not. It returns the boolean value. If the string matches, it returns true otherwise false. It does not take any argument. It does not throw any exception.
Syntax
public boolean matches();
The complete program of java.util.regex.Matcher.matches() method is listed below.
import java.util.regex.*;
public class MatchesMethodExample {
public static void main(String[] args) {
boolean result;
// Get the string value to be checked
String value1 = "CsharpCorner";
// Create a pattern from regex
Pattern pattern = Pattern.compile(value1);
// Get the String value to be matched
String value2 = "CsharpC";
// Create a matcher for the input String
Matcher matcher = pattern.matcher(value2);
// Get the current matcher state
System.out.println("result : " + matcher.matches());
}
}
The above program generates the following output.

public int start() Method
The start() method is used to get the start subsequence index. public int start() method does not take any argument. It returns the index of the first character matched 0. If the operation fails, it throws IllegalStateException.
Syntax
public int start();
The complete program of java.util.regex.Matcher.start() method is listed below.
import java.util.regex.*;
public class StartMethodExample {
public static void main(String[] args) {
// Get the string value to be checked
String value1 = "CsharpCorner";
// Create a pattern from regex
Pattern pattern = Pattern.compile(value1);
// Get the String value to be matched
String value2 = "Csharp";
// Create a matcher for the input String
Matcher matcher = pattern.matcher(value2);
// Get the current matcher state
MatchResult result = matcher.toMatchResult();
System.out.println("Current Matcher: " + result);
while (matcher.find()) {
// Get the first index of match result
System.out.println(matcher.start());
}
}
}
The above program generates the following output.

public boolean find() Method
The find method is used to find the next subsequence of the input sequence that finds the pattern. It returns a boolean value. If the input string matches, then it returns true otherwise returns false. This method does not take any argument. This method does not throw any exception.






Join the conversation! Your thoughts help the community grow.