What is a Regex in Java?
A 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.
import java.util.regex.*;
Java Regex - Matcher
import java.util.regex.Matcher;
The java regex package has three classes:Pattern Class: To define the pattern that needs to be searched.
Matcher Class: To match the pattern in the text and find it.
PatternSyntaxException Class: To indicate any syntax errors in the regular expression.
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
GO TO FULL VERSION