Part of the code I'm working on uses a bunch of regular expressions to search for some simple string patterns (e.g., patterns like "foo[0-9]{3,4} bar"). Currently, we use statically-compiled Java Patterns and then call Pattern#matcher
to check whether a string has contains a match to the pattern (I don't need the match, just a boolean indicating whether there is a match). This is causing a noticeable amount of memory allocation that is affecting performance.
Is there a better option for Java regex matching that is faster or at least doesn't allocate memory every time it searches a string for a pattern?
Regex definitely performs better than String based operations. Java regex engine uses efficient algorithms for finding matches, whereas String.
Expose Literal Characters Regex engines match fastest when anchors and literal characters are right there in the main pattern, rather than buried in sub-expressions. Hence the advice to "expose" literal characters whenever you can take them out of an alternation or quantified expression. Let's look at two examples.
String operations will always be faster than regular expression operations. Unless, of course, you write the string operations in an inefficient way. Regular expressions have to be parsed, and code generated to perform the operation using string operations.
Regex is faster for large string than an if (perhaps in a for loops) to check if anything matches your requirement.
Try matcher.reset("newinputtext")
method to avoid creating new matchers each time you are calling Pattern.matcher.
If you want to avoid creating a new Matcher for each Pattern, use the usePattern()
method, like so:
Pattern[] pats = {
Pattern.compile("123"),
Pattern.compile("abc"),
Pattern.compile("foo")
};
String s = "123 abc";
Matcher m = Pattern.compile("dummy").matcher(s);
for (Pattern p : pats)
{
System.out.printf("%s : %b%n", p.pattern(), m.reset().usePattern(p).find());
}
see the demo on Ideone
You have to use matcher's reset()
method too, or find()
will only search from the point where the previous match ended (assuming the match was successful).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With