• Post Reply Bookmark Topic Watch Topic
  • New Topic

reusable Pattern object  RSS feed

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got this title from this thread's last post

in docs i'v read:

A typical invocation sequence is thus

Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement
boolean b = Pattern.matches("a*b", "aaaaab");
is equivalent to the three statements above, though for repeated matches it is less efficient since it does not allow the compiled pattern to be reused.

i dont understand quite well how can i actually reuse the refered compilation...

i'v a method that's suposed to validate and parse a string to a date; is this the correct way of re-using Pattern class?

or should i instead declare, at top of my class:

and then use p later in my code?

TIA
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I make Patterns as static members of a class:


Once compiled, Pattern objects are immutable and Thread safe so why not?
Bill
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, William
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by miguel lisboa:

Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();

A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement

boolean b = Pattern.matches("a*b", "aaaaab");

i dont understand quite well how can i actually reuse the refered compilation...

i'v a method that's suposed to validate and parse a string to a date; is this the correct way of re-using Pattern class?



The point is efficiency. If you're going to match up a bunch of strings against the same Pattern, it's more efficient to compile() it first. I'm not exactly sure how this particular pattern matcher is implemented, but in general the regular expression gets "compiled" into a Finite State Machine before the test string is compared to it. If you call matches(String,String), the regex will be compiled and then the match will be done.

For example code like...


...will compile, match, compile, match, compile, match.

OTOH, something like...


... will compile, match, match, match.

The second way saves you two compiles. (Granted, the second way incurs a little overhead for making the temporary Matcher objects. But they add up to be considerably less than the compiles that you no longer do.)

As for how to reuse the Pattern once it's compiled, Bill's idea of making it static is good. Any time you want just a single instance of a class, or a few "single" ones that are all different (a date Pattern, a time Pattern, a zip code Pattern, etc.) you could implement the Singleton or Just Create Once pattern. This thread has a nice little discussion of those two patterns.

Ryan
[ April 06, 2005: Message edited by: Ryan McGuire ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!