我对一个小的测试脚本有疑问:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
cfgText = "PATTERN1 = 9\nPATTERN2 = 136.225.73.44\nPATTERN3 = 136.225.236.12"
cfgLine = cfgText.split('\n');
def p = /.*PATTERN2.*/;
def PATTERN2_found = false;
for (i=0; PATTERN2_found==false && i < cfgLine.length; i++)
{
println("cfgLine" +i+ ": " + cfgLine[i]);
def m = cfgLine[i] =~ p;
println("m: " + m)
println("m.asBoolean(): " + m.asBoolean());
println("m: " + m)
println("m.asBoolean(): " + m.asBoolean());
if(m.asBoolean()){
println("Heeeyyyy");
}
println("--------------------------------");
}
这是它的输出:
cfgLine0: PATTERN1 = 9
m: java.util.regex.Matcher[pattern=.*PATTERN2.* region=0,12 lastmatch=]
m.asBoolean(): false
m: java.util.regex.Matcher[pattern=.*PATTERN2.* region=0,12 lastmatch=]
m.asBoolean(): false
--------------------------------
cfgLine1: PATTERN2 = 136.225.73.44
m: java.util.regex.Matcher[pattern=.*PATTERN2.* region=0,24 lastmatch=]
m.asBoolean(): true
m: java.util.regex.Matcher[pattern=.*PATTERN2.* region=0,24 lastmatch=PATTERN2 = 136.225.73.44]
m.asBoolean(): false
--------------------------------
cfgLine2: PATTERN3 = 136.225.236.12
m: java.util.regex.Matcher[pattern=.*PATTERN2.* region=0,25 lastmatch=]
m.asBoolean(): false
m: java.util.regex.Matcher[pattern=.*PATTERN2.* region=0,25 lastmatch=]
m.asBoolean(): false
--------------------------------
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedConstructor$1 (file:/usr/share/groovy/lib/groovy-2.4.15.jar) to constructor java.util.regex.Matcher()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedConstructor$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
如您所见,正则表达式在第二个循环中匹配,但是行为对我来说很奇怪。我真的不知道为什么我对同一个Matcher对象使用两次asBoolean
会导致结果不同。它具有内部迭代器还是类似的东西?
PS:我已经使用==~
运算符解决了这个问题,但是我想知道为什么asBoolean可以这样工作。
答案 0 :(得分:2)
发生这种情况是因为StringGroovyMethods.asBoolean(Matcher matcher)
调用matcher.find()
会修改匹配器的内部状态。
/**
* Coerce a Matcher instance to a boolean value.
*
* @param matcher the matcher
* @return the boolean value
* @since 1.7.0
*/
public static boolean asBoolean(Matcher matcher) {
if (null == matcher) {
return false;
}
RegexSupport.setLastMatcher(matcher);
return matcher.find();
}
来源:src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
这就是为什么当您第一次调用m.asBoolean()
时会返回true
的原因,因为它在此调用之前的状态为(未找到匹配项):
m: java.util.regex.Matcher[pattern=.*PATTERN2.* region=0,24 lastmatch=]
现在,当您第二次调用m.asBoolean()
时,匹配对象将由上一次调用修改,并由以下内容表示:
m: java.util.regex.Matcher[pattern=.*PATTERN2.* region=0,24 lastmatch=PATTERN2 = 136.225.73.44]
它返回false
,因为没有其他部分满足匹配器。