In this example we will show how to use String.matches() in Java.
Source Code
package com.beginner.examples;
public class MatchesExample {
public static void main(String a[]){
String str = "1";
//String.matches() method tells whether or not this string matches the given regular expression.
System.out.println(str.matches("(\d+)"));
str = "1a";
//String.matches() method tells whether or not this string matches the given regular expression.
System.out.println(str.matches("(\d+)"));
}
}
Output:
true
false