Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String matches() method
Syntax and Examples



Introduction

The matches() method in Java is a powerful tool for validating strings against regular expressions. It checks if the entire input string conforms to the pattern defined by the regex.

Syntax

    public boolean matches(String regex)
  

Parameters

Parameter Description
regex The regular expression to which the string is matched.

Return Value

This method returns true if the entire sequence of characters in the input string matches the given regular expression; otherwise, it returns false.

Examples

Example 1: Basic Pattern Matching

Let's start with a simple example. This example demonstrates how to check if a string consists only of digits using the regex "\d+".

    
public class MatchesExample1 {
  public static void main(String[] args) {
    String str1 = "12345";
    String str2 = "abc123";
    System.out.println("" + str1 + " matches: " + str1.matches("\d+")); // Matches only digits
    System.out.println("" + str2 + " matches: " + str2.matches("\d+"));
  }
}
  

12345 matches: true
abc123 matches: false

In this example, str1 contains only digits, so it successfully matches the regex. str2 contains both letters and numbers, failing the match.

Example 2: Matching a Specific Format (Email)

This example shows how to validate an email address using a simplified regular expression for emails.

    
public class MatchesExample2 {
  public static void main(String[] args) {
    String email1 = "test@example.com";
    String email2 = "invalid-email";

    System.out.println("" + email1 + " matches: " + email1.matches("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"));
    System.out.println("" + email2 + " matches: " + email2.matches("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"));
  }
}
  

test@example.com matches: true
invalid-email matches: false

The regex [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} is a simplified pattern for emails. It's important to note that a truly robust email validation requires a much more complex regular expression, as the rules are quite intricate.

Example 3: Using Character Classes

This example uses character classes to match strings containing only lowercase letters.

    
public class MatchesExample3 {
  public static void main(String[] args) {
    String str1 = "lowercase";
    String str2 = "MixedCase";

    System.out.println("" + str1 + " matches: " + str1.matches("[a-z]++"));
    System.out.println("" + str2 + " matches: " + str2.matches("[a-z]++"));
  }
}
  

lowercase matches: true
MixedCase matches: false

The regex [a-z]+ specifies that the string must contain only lowercase letters. Therefore, "lowercase" matches, while "MixedCase" does not.

Example 4: Matching with Quantifiers

This example uses quantifiers to match strings that start with one or more digits followed by some characters.

    
public class MatchesExample4 {
  public static void main(String[] args) {
    String str1 = "123abc";
    String str2 = "abc";
    System.out.println("" + str1 + " matches: " + str1.matches("\d+.*+"));
    System.out.println("" + str2 + " matches: " + str2.matches("\d+.*+"));
  }
}
  

123abc matches: true
abc matches: false

The regex \d+.∗+ requires at least one digit (\d+) followed by any characters (.∗+). Thus, “123abc” successfully matches because it starts with digits.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M