Java String matches Method

The Java String matches method tells whether the string matches the user-specified regular expression or not. Based on the result, it will return Boolean True or False.

The basic syntax of the string matches in Java Programming language is as shown below. The following matches method will accept the Regular expression as the argument and check whether the original string matches this expression or not.

public boolean matches(String regexp); // It will return boolean True or False 

//In order to use in program
String_Object.matches(String regexp);

Java String matches Example

In this program, we use the String.matches method to check whether this string is exactly matching with the user-specified expression or not.

// Java String.matches example
package StringFunctions;

public class MatchMethod {
	public static void main(String[] args) {
		String str = "Learn Java at Tutorial Gateway";
		String str1 = "We are abc working at abc Company";
		String str2 = "We are abc working at abc Company";
		
		boolean bool1 = str1.matches("abc");
		boolean bool2 = str1.matches(str2);		
		boolean bool3 = str.matches("Learn Java(.*)");
		boolean bool4 = str.matches("(.*)Java(.*)");

		System.out.println("Does String str1 Matches with 'abc'? = " + bool1);
		System.out.println("Does String str1 Matches with str2? = " + bool2);
		System.out.println("Does String str1 Matches with RegExp? = " + bool3);
		System.out.println("Does String str1 Matches with RegExp? = " + bool4);
	}
}
Java String Matches Method 1

The following statement will call the public boolean matches (String regex) method to check whether the string str1 matches with “abc” or not. From the above screenshot, you can observe that it is returning False because they are not matching.

boolean bool1 = str1.matches("abc");

It will check whether the string str eqauls with “Learn Java (.*)” or not. It means the string should start with Learn Java, and it will accept anything after that.

boolean bool3 = str.matches("Learn Java(.*)");

The following statement will check whether the string str matches with “(.*)Java (.*)” or not. It means the string must contain Java, and it will accept anything before or after the java word.

boolean bool4 = str.matches("(.*)Java(.*)");

Lastly, we used the Java System.out.println statements will print the output.

Java String matches Example

In this Java program, We are going to ask the user to enter any word. Based on the user entered string value, this String Function will display the message.

package StringFunctions;
import java.util.Scanner;

public class MatchMethodex {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);		
		System.out.println("Please Enter any word: ");
		String str = sc.nextLine();
		
		if (str.matches("(.*)java(.*)")) {
			System.out.println("Hey!! Welcome to Java Programming Language");
		}
		else {
			System.out.println("Goodbye Tutorial Gateway");
		}
	}
}
Please Enter any word: 
Searching for java string functions
Hey!! Welcome to Java Programming Language

Let us enter different word

Please Enter any word: 
Search SQL server
Goodbye Tutorial Gateway

Within this String matches example, the following statement will ask the user to enter any word. Then we are going to assign the user entered value to string variable str.

System.out.println("Please Enter any word: ");
String str = sc.nextLine();

Next, we used the If Else Statement to check whether the user entered string matches with “(.*)Java (.*)” or not. It means the string must contain Java, and it will accept anything before or after the java word.

  • If the statement inside the If Statement is True, System.out.println(“Hey!! Welcome to Java Programming Language”); statement printed.
  • Else System.out.println(“Goodbye to Java Tutorials”); statement printed.