Java - String matches() Method



The Java String matches() method is used to check whether or not the current string matches the given regular expression. A regular expression is a pattern that instructs a string to only contain numbers or alphabetical characters. Any character set or pattern may be used for the regular expression.

For instance, we frequently have to build code to determine whether a string is numeric or whether it contains letters; determining whether a string contains a specific numeral four times; does string consists of alphabets like A to Z or a to z, etc. Java programming has numerous situations where regular expressions are required.

Syntax

Following is the syntax for Java String matches() method −

public boolean matches(String regex)

Parameters

  • regex − This is the regular expression to which this string is to be matched.

Return Value

This method returns true if, and only if, this string matches the given regular expression.

Example

The following example shows the usage of Java String matches() method. Here we are creating two Strings 'str1' and 'str2' with values "tutorials" and "learning" respectively. Thereafter, the given strings are matched with the provided regular expressions −

import java.lang.*; 
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials", str2 = "learning";       
      boolean retval = str1.matches(str2);      
      
      // method gets different values therefore it returns false
      System.out.println("Value returned = " + retval); 
      retval = str2.matches("learning");      
      
      // method gets same values therefore it returns true
      System.out.println("Value returned = " + retval);    
      retval = str1.matches("tuts");      
      
      // method gets different values therefore it returns false
      System.out.println("Value returned = " + retval);
   }
}

Output

Let us compile and run the above program, this will produce the following result:

Value returned = false
Value returned = true
Value returned = false

Example

We have defined a regular expression for number matching in the program below. The result "true" is returned when the string to be matched contains only numbers and, "false" is returned when alphabets and numerals; numerals and characters are combined because it doesn't match the regular expression.

public class StringDemo {
   public static void main(String[] args) {
      String RegexExpression = "^[0-9]+$";
      System.out.println("The returned value is: " + "8785".matches(RegexExpression));
      System.out.println("The returned value is: " + "@%*6575".matches(RegexExpression));
      System.out.println("The returned value is: " + "675hjh".matches(RegexExpression));
   }
}

Output

Let us compile and run the program above, the output will be displayed as follows −

The returned value is: true
The returned value is: false
The returned value is: false

Example

We will now create a program to determine whether a string has only alphabetic characters or not. For this, the regex that includes "A to Z", "a to z" and one blank space (' ') should be given because a string could include several words.

public class StringDemo {
   public static void main(String[] args) {
      String RegexExpression = "[a-zA-Z ]+";
      String s1 = "Tutorials Point is a reputed firm";
      String s2 = "Tutori@ls && Po!nt is @ reputed f!rm";
      String s3 = "Tutorials Point is a reputed firm 766868";
      boolean Match1 = s1.matches("[a-zA-Z ]+");
      boolean Match2 = s2.matches("[a-zA-Z ]+");
      boolean Match3 = s3.matches("[a-zA-Z ]+");
      System.out.println("Is the string having only alphabets: " + Match1);
      System.out.println("Is the string having only alphabets: " + Match2);
      System.out.println("Is the string having only alphabets: " + Match3);
   }
}

Output

On executing the program above, the output is obtained as follows:

Is the string having only alphabets: true
Is the string having only alphabets: false
Is the string having only alphabets: false

Example

The Regular expression is incorrectly defined in the programme given below. An exception is raised because the syntax is incorrect −

public class StringDemo {
   public static void main(String[] args) {
      String s = "Coding";
      String regexExpression = "[Cod...!!";
      
      // printing the result
      System.out.println("The returned value is: " + s.matches(regexExpression));
   }
}

RuntimeException

The output for the program above is obtained as follows:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 8
[Cod...!!
      ^
      at java.base/java.util.regex.Pattern.error(Pattern.java:2038)
      at java.base/java.util.regex.Pattern.clazz(Pattern.java:2700)
      at java.base/java.util.regex.Pattern.sequence(Pattern.java:2149)
      at java.base/java.util.regex.Pattern.expr(Pattern.java:2079)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1793)
      at java.base/java.util.regex.Pattern.<init>(Pattern.java:1440)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1079)
      at java.base/java.util.regex.Pattern.matches(Pattern.java:1184)
      at java.base/java.lang.String.matches(String.java:2838)
      at StringDemo.main(StringDemo.java:6)
java_lang_string.htm
Advertisements