String has a convenient method matches(String regex)
. But I am about to check about 10-100 values for match. I don't think it's very effective to let Java compile the pattern for every call. Instead, I'd like to cache parsed Pattern and use that.
But how can I, staying effective as much as possible, use Pattern object along with String to produce boolean
indicating that the string matches the pattern?
public static boolean patternMatches(String tested, Pattern regex) {
???
}
Second reason why I want to do this is that I already have a method that takes string and retrieves literal string matchs:
public MyClass[] findMatches(String substring) {
...
}
So I wanted to make an overload:
public MyClass[] findMatches(Pattern regex) {
...
}