以下正则验证返回不同的结果,请大家帮忙看下问题,使用的是org.apache.oro.text.regex这个包
[code="java"]PatternCompiler compiler = new Perl5Compiler();
PatternCompiler compiler2 = new Perl5Compiler();
try {
Pattern p = compiler.compile(
".*\\s(and|or)\\s+.*([>=<]|\\slike\\s|\\sbetween\\s)+.*",
Perl5Compiler.READ_ONLY_MASK);
PatternMatcherInput input = new PatternMatcherInput(
"30589 and 7659=7659");
PatternMatcher matcher = new Perl5Matcher();
System.out.println(matcher.contains(input, p)); //这个返回true
System.out.println(matcher.contains(input, compiler2.compile(
".*\\s(and|or)\\s+.*([>=<]|\\slike\\s|\\sbetween\\s)+.*",
Perl5Compiler.READ_ONLY_MASK))); //这个返回false
} catch (MalformedPatternException e) {
System.out.println("error");
}[/code]
API用的不对,参考:
[quote]
public boolean contains(PatternMatcherInput input,
Pattern pattern)
Determines if the contents of a PatternMatcherInput, starting from the current offset of the input contains a pattern. If a pattern match is found, a MatchResult instance representing the first such match is made acessible via getMatch(). The current offset of the PatternMatcherInput is set to the offset corresponding to the end of the match, so that a subsequent call to this method will continue searching where the last call left off. You should remember that the region between the begin and end offsets of the PatternMatcherInput are considered the input to be searched, and that the current offset of the PatternMatcherInput reflects where a search will start from. Matches extending beyond the end offset of the PatternMatcherInput will not be matched. In other words, a match must occur entirely between the begin and end offsets of the input. See PatternMatcherInput for more details.
[/quote]
把
PatternMatcherInput input = new PatternMatcherInput(
"30589 and 7659=7659");
换成String 类型就可以了
String input="30589 and 7659=7659";
PatternMatcherInput匹配一次后,offset就变了