What's the use in java.util.regex.Pattern and java.util.regex.Matcher design?

It seems java.util.regex.Pattern and java.util.regex.Matcher are relationship aggregation I guess. And the Java API said to use them below.

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

      

Why are Pattern and Matcher initialized with a static method? What is the use of such an opportunity? Does it have the disadvantage of a call originator in my origin directly?

+3


source to share


1 answer


Why Pattern

is it created by a static

factory method ?

According to the documentationPattern

,

A (Pattern) is a compiled regex representation.

Object A Pattern

will be associated with a template, and users of those objects must create it once and use it many times. By providing a static class factory Pattern

is allowed to perform internal checks before returning the object Pattern

. For example, it can (if it wants to) cache instances Pattern

and return a cached instance if the same template string is provided in another call compile

(Note: this is not how it is implemented, however, it has this freedom due to its use of a static factory) ...

Why Matcher

is it created with a factory method on Pattern

?

Matcher

can be used for two purposes
(Below is a simplified perspective for discussion, see Java docMatcher

for more details)
:



  • Check if the given string matches the given regular expression.
  • Match the given string against the given pattern and return different matching results.

In the first case, you can use the method call form Pattern.matches(regex, string)

. In this case the regex will be compiled and a boolean result will be returned after the match. Note that this is kind of a programming style functional

- and it works great here because there is no corresponding state.

In the second case, the matching state must be maintained, which the user can request after the matching is done. Hence, in this case, an object is used Matcher

that can maintain the state of the match results. Since an object Matcher

cannot exist without a corresponding object Pattern

, the API developer only allows it to be instantiated Pattern

- thus, users must call p.matcher('aaaaab')

. The inner code in the class Pattern

looks like this:

public Matcher matcher(CharSequence input) {
    if (!compiled) {
        synchronized(this) {
            if (!compiled)
                compile();
        }
    }
    Matcher m = new Matcher(this, input);
    return m;
}

      

As you can see, it Matcher

takes Pattern

as a parameter to a constructor so that it can refer to it at different points to get and maintain the result of the match

PS
As with any API, tags Pattern

and Matcher

could be implemented in different ways - not all Java APIs are consistent in their design - I think there is always some feature of the developer who developed these APIs. The above answer is my interpretation of the approach these developers have taken.

+4


source







All Articles