简体   繁体   English

java 正则表达式匹配器是有状态的吗?

[英]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.我来自 python 方面,对 java 正则表达式了解不多,这个问题很容易解释,让我添加一些场景。

Assume I have an instance with a Matcher matcher variable, and a function like this:假设我有一个带有Matcher matcher变量和 function 的实例,如下所示:

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?在命名所有正则表达式捕获组的位置,多次调用它会导致问题吗?

  1. Yes Matcher is stateful.是的Matcher是有状态的。

  2. If anything 1 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.如果在您(仍在)查看上一次呼叫中的组(等)时findmatch任何1个呼叫,那么您将丢失上一次呼叫中的 state。 The same applies for reset and reset(CharSequence) , and some other methods.这同样适用于resetreset(CharSequence)以及其他一些方法。 This behavior is inherent in the API design, and clearly documented.这种行为是 API 设计中固有的,并且有明确的记录。

  3. Matcher is not thread-safe. Matcher不是线程安全的。 The javadoc states this explicitly: javadoc明确说明了这一点:

    "Instances of this class are not safe for use by multiple concurrent threads." “这个 class 的实例对于多个并发线程使用是不安全的。”

  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.但是,像您的代码一样使用它应该可以工作……前提Matcher仅对当前线程可见/由当前线程使用,并且不能在调用堆栈的上方(或下方)进一步使用。

See also:也可以看看:


By contrast, Pattern is both thread-safe and immutable / stateless.相比之下, Pattern既是线程安全的又是不可变/无状态的。


1 - That could be another thread, or the current thread that is using the same Matcher at different points in the call stack; 1 - 这可能是另一个线程,或者是在调用堆栈的不同点使用相同Matcher的当前线程; ie via recursion or something like that.即通过递归或类似的东西。

Pattern is thread-safe, but Matcher is not. Pattern是线程安全的,但Matcher不是。

Matcher maintains some local variables like groupVars , localVars , last etc. Matcher维护一些局部变量,如groupVarslocalVarslast等。

groupVars is used for recording group captured, it will be reset before each Matcher#match and Matcher#find operation. groupVars用于记录捕获的组,它会在每个Matcher#matchMatcher#find操作之前被重置。

localVars is used for recording the context of matching operation. localVars用于记录匹配操作的上下文。

last is used by find , it represents the terminal offset of previous find operation. lastfind使用,它表示上一次find操作的终端偏移量。

If you use Matcher concurrently, those local variables will be covered by different thread, which could cause unexpected result.如果同时使用Matcher ,这些局部变量将被不同的线程覆盖,这可能会导致意外结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM