If I would want to make a 100% clone of String#contains(CharSequence s): boolean
in Java regex using Pattern
. Would the following calls be identical?
input.contains(s);
and
Pattern.compile(".*" + Pattern.quote(s) + ".*").matcher(input).matches();
Similarly, would the following code have the same functionality?
Pattern.compile(Pattern.quote(s)).matcher(input).find();
I presume that the regex search is less performant, but only by a constant factor. Is this correct? Is there any way to optimize the regular expressions to mimic contains
?
The reason that I'm asking is that I have a piece of code that is written around Pattern
and it seems wasteful to create a separate piece of code that uses contains
. On the other hand, I don't want different test results - even minor ones - for each code. Are there any Unicode related differences, for instance?
If you need to write a .contains
like method based on Pattern
, you should choose the Matcher#find()
version:
Pattern.compile(Pattern.quote(s)).matcher(input).find()
If you want to use .matches()
, you should bear in mind that:
.*
will not match line breaks by default and you need(?s)
inline modifier at the start of the pattern or usePattern.DOTALL
option- The
.*
at the pattern start will cause too much backtracking and you may get a stack overflow exception, or the code execution might just freeze.