Java String matches() Method

The String.matches() method in Java is used to check if a string matches a specified regular expression. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. matches Method Syntax
  3. Examples
    • Checking for a Simple Match
    • Using Special Characters in Regular Expressions
    • Validating Input Formats
  4. Conclusion

Introduction

The String.matches() method is a member of the String class in Java. It allows you to determine if the entire string matches a specified regular expression, which is useful for validation and pattern matching tasks.

matches Method Syntax

The syntax for the matches method is as follows:

public boolean matches(String regex)
  • regex: The regular expression to which the string is to be matched.

Examples

Checking for a Simple Match

The matches method can be used to check if a string matches a simple regular expression.

Example

public class MatchesExample {
    public static void main(String[] args) {
        String str = "hello";

        boolean matches = str.matches("hello");

        System.out.println("Does 'hello' match 'hello'? " + matches);
    }
}

Output:

Does 'hello' match 'hello'? true

Using Special Characters in Regular Expressions

The matches method can be used with regular expressions that include special characters.

Example

public class MatchesExample {
    public static void main(String[] args) {
        String str = "hello123";

        boolean matches = str.matches("\\w+\\d+");

        System.out.println("Does 'hello123' match '\\w+\\d+'? " + matches);
    }
}

Output:

Does 'hello123' match '\w+\d+'? true

Validating Input Formats

The matches method is useful for validating input formats, such as email addresses, phone numbers, and other patterns.

Example: Validating an Email Address

public class MatchesExample {
    public static void main(String[] args) {
        String email = "example@example.com";

        boolean isValidEmail = email.matches("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,6}$");

        System.out.println("Is 'example@example.com' a valid email? " + isValidEmail);
    }
}

Output:

Is 'example@example.com' a valid email? true

Example: Validating a Phone Number

public class MatchesExample {
    public static void main(String[] args) {
        String phoneNumber = "123-456-7890";

        boolean isValidPhoneNumber = phoneNumber.matches("^\\d{3}-\\d{3}-\\d{4}$");

        System.out.println("Is '123-456-7890' a valid phone number? " + isValidPhoneNumber);
    }
}

Output:

Is '123-456-7890' a valid phone number? true

Conclusion

The String.matches() method in Java provides a powerful way to check if a string matches a specified regular expression. By understanding how to use this method, you can efficiently perform pattern matching and validation tasks in your Java applications. Whether you are checking for simple matches, using special characters in regular expressions, or validating input formats, the matches method offers a reliable solution for these tasks.

Comments