Prev Next

Java / Regular expressions

1. 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() match...

Read full answer

2. Explain the difference between String.matches and Matcher.matches.

Matcher.matches() is good in terms of performance as a Matcher is created on a precompiled regular expressions, while the String.matches() recompiles the regular expression every time it executes.

Read full answer

3. Is Java Regex Thread Safe?

Instances of Pattern class are immutable and are safe for use by multiple concurrent threads. Instances of the Matcher class are not thread safe.

Read full answer

«
»

Comments & Discussions