JDK-6565414 : Optimize dot-star regex pattern matching
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util.regex
  • Affected Version: 6
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-06-04
  • Updated: 2010-04-04
Description
A DESCRIPTION OF THE REQUEST :
For the regex pattern ".*" (i.e. dot-star), we know that the match result -- find() or matches() -- is always going to be true. This and such cases can be highly optimized rather than actually evaluating the regex to find the result.

JUSTIFICATION :
This will add more performance to regex evaluation when matching against a specific set of patterns e.g. .*

We are using regex in our project extensively, and there is this one case where we don't want the dot-star regex pattern to be evaluated because everybody knows regex evaluation is costly. Given the dot-star pattern, we simply want to return the entire input string. To do this we have to use a lot of if-else in our code which makes it cluttered.

Therefore, we were wondering if the Matcher itself could optimize the evaluation of this (and such) patterns, then the application doesn't have to worry about it. I saw the source code of Pattern and Matcher, but they don't do any such optimization. Pattern.compile() can be tweaked to compile such
patterns so that subsequently when find()/match() is called, it simply returns true.