Help the world stop coronavirus! Stay home!

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());
	}
}

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

Show more question and Answers...

Design Patterns

Comments & Discussions