Strings are a part of every programming language. They are like an array of characters that are used according to the users’ requirements while coding. The strings are used for the representation of text data. In Java, strings are also considered as the sequence of characters. The strings class offers several in-built methods that can be utilized according to the scenario. One of the methods is the matches() of the Java’s string class.
In this write-up, we will demonstrate the working of the string matches() method in Java.
How to Use String matches() in Java?
The “string.matches()” function in Java depicts whether the declared string matches with the regular expression(Regex) or not. This method in Java is of boolean type and returns a value of “true” or “false” based on the provided input.
There are two other variants of the string matches() method which are the String regionMatches() and the String region Matches with ignore case.
The syntax of the string.matches() is depicted in the below snippet.
public boolean matches (String regex) |
In the above syntax,
- A boolean value is returned true if the string matches otherwise false is returned.
- The string is to be matched with the regex.
Now we will move towards the code examples of the string matches() method in Java.
Example 1: Basic Implementation of the String matches() Method in Java
A simple implementation of the string matches() method in Java is shown below:
//Declare the class
public class MatchExample1 {
// main() method of Java
public static void main(String args[])
{ //Declare the Strings
String S = new String("Programming in Java");
System.out.print("Is there (.*) Java? ");
//Use the match() method to match with the Regex
System.out.println(S.matches("(.*)Java"));
System.out.print("Is there Programming? ");
System.out.println(S.matches("Programming"));
}
}
In the above Java code:
- The class in Java is declared as “MatchExample1”.
- The string object is declared as “S” in the next step.
- The “.*” regex is used within the matches() function to check if “Java” appears anywhere in the string.
- After that, the same function is used to check the existence of the string “Programming”.
Output
The output below shows that “Java” exists in the string whereas, “Programming” does not match anywhere in the string.

Example 2: Using the Anchoring Operations
The matches() method can be implemented using the anchoring element using the “^” operator at the beginning of the string and the “$” operator at the end of the string to match the string pattern declared. Here is a practical demonstration of the stated concept.
class MatchExample2 {
public static void main(String[] args) {
//A STring Starting with Character P and ending at n
String matchreg = "^P....n$";
//Print the output
System.out.println("Does the String 'Python' match with the Regex ? ");
System.out.println("Python".matches(matchreg));
}
}
In the above Java code:
- The class is declared as “MatchExample2”.
- In the main()method, the string “matchreg” is declared with the “^” operator that depicts the beginning of the string and the “$” operator that represents the ending of the string.
- The output is printed using the println() method with the matches() method to check if the string matches or not.
Example 3: Matching the String Using the List Operator
The list operators of Regular expressions are implemented using the opening list operator “[” and the closing list operator “]”. The code below shows that the list operators are used to perform case-sensitive searches in the string.
//Declare the class to implement the matches() methodclass MatchExample3 {
public static void main(String[] args) {
//Declare a String
String w = "JAVAPROGRAMMING";
//Print the output
System.out.println("The Output is: " + w.matches("[A-Z]*") );
}
}
In the above code block:
- The class is declared as “MatchExample3”.
- The string is described as “JAVAPROGRAMMING” which is matched with the regular expression using the list operator.
- The output is printed accordingly using the matches() method declared in the println() method of Java.
Output
The output below shows that a value of “true” is returned since “JAVAPROGRAMMING” matches with the declared Regex.

This article will further advance to discuss the exceptions of the string matches() method in Java.
Exceptions While Working With the String matches() Method of Java
There are two exceptions raised while working with the matches() method in Java as depicted in the code examples below.
Exception 1: NullPointerException Using the matches() Method of Java
The code below shows that when a null is tried to match with the declared string an exception is raised by the matches() method of Java.
class MatchException1 {
public static void main(String[] args) {
String w = "JAVAPROGRAMMING";
System.out.println(w.matches(null));
}
}
In the above code block:
- The declared string is matched with “null” using the matches() method.
- The println() method prints the result.
Output
The output below shows that the exception in Java is raised as “NullPointerException”. Along with this exception the String.is.Empty() is also raised for the string that contains a null value.

Exception 2: PatternSyntaxException Using the matches() Method of Java
The code below shows that whenever the Regex is declared in a wrong manner, an exception is raised in the code.
class MatchException2 {
public static void main(String[] args) {
String w = "JAVAPROGRAMMING";
System.out.println("The Output is: " + w.matches("[A-Z*") );
}
}
In this code block:
- The matches() method is declared with an unclosed list operator “]”.
- The println() method prints an exception as per the code.
Output
The output below shows that the “PatternSyntaxException” is raised when an unclosed character is declared.

This concludes the discussion on the string matches() method in Java.
Conclusion
The matches() method of string in Java matches the regex with the specified string. It retrieves the output in a boolean type as true or false. This method raises certain exceptions for invalid inputs. In this article, we have implemented different code examples to elaborate on the working of the matches() method in Java.