Prev Next

Java / Regular expressions

What is the difference between matches() and find() in Java Regex?

matches() matches the expression against the entire string as it implicitly add a ^ at the start and $ at the end of your pattern, so it will not match substring or part of the string. ^ and $ are meta characters that represents start of the string and end of the string respectively.

find() matches the next occurrence within the substring that matches the regex.

public class PatternMatchExample {

	public static void main(String[] args) {
		Pattern p = Pattern.compile("[a-zA-Z]{9}");
		Matcher m = p.matcher("www.javapedia.net");
		System.out.println("Javapedia matched :" + m.matches());
		System.out.println("Javapedia found :" + m.find());
	}
}

More Related questions...

Show more question and Answers...


Comments & Discussions