javaregexstateful

Is java regex matcher stateful?


I come from the python side and does not know much about java regex, the question is pretty self explanatory, let me add some scenario.

Assume I have an instance with a Matcher matcher variable, and a function like this:

public String getMatch(String group) {
    if (matcher.find()) {
        return matcher.group(group);
    } else { blah }
}

Where all regex capturing groups are named, Would calling it multiple time cause problems?


Solution

    1. Yes Matcher is stateful.

    2. If anything1 calls find or match while you are (still) looking at the groups (etc) from the previous call, then you will lose the state from the previous call. The same applies for reset and reset(CharSequence), and some other methods. This behavior is inherent in the API design, and clearly documented.

    3. Matcher is not thread-safe. The javadoc states this explicitly:

      "Instances of this class are not safe for use by multiple concurrent threads."

    4. However, using it like your code does should work ... provided that the Matcher was only visible to / used by the current thread, and not used further up (or down) the call stack.

    See also:


    By contrast, Pattern is both thread-safe and immutable / stateless.


    1 - That could be another thread, or the current thread that is using the same Matcher at different points in the call stack; i.e. via recursion or something like that.