0

From PCRE Introduce

This extension maintains a global per-thread cache of compiled regular expressions (up to 4096).

How about java.util.regex, will this cache the compiled patterns?

Because sometimes I just don't want to introduce too many variables to store patterns.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
wener
  • 7,191
  • 6
  • 54
  • 78

2 Answers2

2

How about java.util.regex, will this cache the compiled patterns?

Not in Java 6 or 7 ... according to the source code.

My memory was that the static Pattern.compile(...) methods did implement a small cache, but presumably that feature was removed in / by Java 6.

If you want to implement Pattern caching in Java, you can do it yourself in a variety of ways. (Alternatively, Googling for "java regex cache" gave me some useful hits ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

If you create a Pattern object, it compiles the regular expression, and as long as you hold that object, it is "cached" for further use. You can make your application even more efficient by using a single Matcher object.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108