Question - What is difference between matches() and find() in Java Regex?
Answer -
matches() returns true only if the whole string matches the specified pattern while find() returns trues even if a substring matches the pattern.
import java.util.regex.*;
public class RegexTutorial {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\d");
String test = "JavaInUse123";
Matcher m = pattern.matcher(test);
if (m != null){
System.out.println(m.find());
System.out.println(m.matches());
}
}
}