Java.lang.String.matches() in Java

There are three variants of matches() method. This article depicts about all of them, as follows:
1. String matches() : This method tells whether or not this string matches the given regular expression. An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str).

Syntax:
public boolean matches(String regex)
Parameters
regex : 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the
// working of matches() 
public class Match1 {
      
   public static void main(String args[]) {
        
      // Initializing String 
      String Str = new String("Welcome to geeksforgeeks");
        
      // Testing if regex is present
      System.out.print("Does String contains regex (.*)geeks(.*) ? : " );
      System.out.println(Str.matches("(.*)geeks(.*)"));
        
      // Testing if regex is present
      System.out.print("Does String contains regex geeks ? : " );
      System.out.println(Str.matches("geeks"));
        
   }
}

chevron_right


Output:



Does String contains regex (.*)geeks(.*) ? : true
Does String contains regex geeks ? : false

2. String regionMatches() (with ignoreCase) :This method has two variants which can be used to test if two string regions are equal.

Syntax
public boolean regionMatches(boolean ignoreCase,
                             int str_strt,
                             String other,
                             int other_strt,
                             int len)
Parameters
str_strt : the starting offset of the subregion in this string.
other : the string argument.
other_strt :  the starting offset of the subregion in the string argument.
len : the number of characters to compare.
ignoreCase :  if true, ignore case when comparing characters.
Return Value
It returns true if the specified subregion of this string
matches the specified subregion of the string argument;
false otherwise.
Whether the matching is exact or case insensitive depends on the ignoreCase argument.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the
// working of regionmatches()
public class Match2 {
      
   public static void main(String args[]) {
        
      // Initializing String 
      String Str1 = new String("Welcome to geeksforgeeks");
        
      // Initializing test String
      String Str2 = new String("GEEKS");
        
      // Tests whether GEEKS starts in geeksforgeeks starting from pos 11
      // and from 0 ( i.e starting in GEEKS) and ignores case
      // and compares 5 characters of GEEKS
      System.out.print("Checking if GEEKS is in geeksforgeeks( case insensitive ) : " );
      System.out.println(Str1.regionMatches(true, 11, Str2, 0, 5));
        
   }
}

chevron_right


Output:

Checking if GEEKS is in geeksforgeeks( case insensitive ) : true

3. String regionMatches() :This method has two variants which can be used to test if two string regions are equal.

Syntax
public boolean regionMatches(int str_strt,
                             String other,
                             int other_strt,
                             int len)
Parameters
str_strt : the starting offset of the subregion in this string.
other : the string argument.
other_strt :  the starting offset of the subregion in the string argument.
len : the number of characters to compare.
Return Value
It returns true if the specified subregion of this string
matches the specified subregion of the string argument;
false otherwise.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the
// working of regionmatches()
public class Match3 {
      
   public static void main(String args[]) {
        
      // Initializing String 
      String Str1 = new String("Welcome to geeksforgeeks");
        
      // Initializing test String
      String Str2 = new String("GEEKS");
        
      // Tests whether GEEKS starts in geeksforgeeks starting from pos 11
      // and compares 5 characters of GEEKS
      System.out.print("Checking if GEEKS is in geeksforgeeks( case sensitive ) : " );
      System.out.println(Str1.regionMatches(11, Str2, 0, 5));
        
   }
}

chevron_right


Output:

Checking if GEEKS is in geeksforgeeks( case sensitive ) : false

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up