I have a regex that works great (500 nanoseconds) when a match is found, but takes a long time (more than 3 seconds) when there is no match. I suspect this may be due to indentation. I tried several options, for example, converting .*in (.*)?based on some documentation, but that did not help.
Input: a very long string - 5k characters in some cases.
Regular expression: .*substring1.*substring2.*
I precompile the template and reuse the helper, what else can I try?
Here is my piece of code - I will call this method millions of different input lines, but only a few regex patterns.
private static HashMap<String, Pattern> patternMap = new HashMap<String, Pattern>();
private static HashMap<String, Matcher> matcherMap = new HashMap<String, Matcher>();
Here is my method:
public static Boolean regex_match(String line, String regex) {
if (regex == null || line == null) {
return null;
}
if (!patternMap.containsKey(regex)) {
patternMap.put(regex, Pattern.compile(regex));
matcherMap.put(regex,patternMap.get(regex).matcher(""));
}
return matcherMap.get(regex).reset(line).find(0);
}