⬅ Previous Topic
Java Stringlength()
methodNext Topic ⮕
Java StringoffsetByCodePoints()
methodmatches()
method⬅ Previous Topic
Java Stringlength()
methodNext Topic ⮕
Java StringoffsetByCodePoints()
methodThe 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.
public boolean matches(String regex)
Parameter | Description |
---|---|
regex |
The regular expression to which the string is matched. |
This method returns true
if the entire sequence of characters in the input string matches the given regular expression; otherwise, it returns false
.
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.
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.
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.
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.
⬅ Previous Topic
Java Stringlength()
methodNext Topic ⮕
Java StringoffsetByCodePoints()
methodYou 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.