我尝试使用useTransparentBounds()
,但它似乎没有按预期工作(如ideone 所示)。在以下代码段中,我希望m.find()
找到匹配项,因为启用了透明边界,因此允许在Matcher
其区域边界之外进行搜索。为什么这不起作用?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Foo {
public static void main(String[] args) {
// match everything preceded by X
Matcher m = Pattern.compile(".*(?<=X)").matcher("Foo BarX Baz");
// limit matcher to first chars outside of normal lookahead scope
m.region(0, 4);
// matcher should still find a match because of transparent bounds
m.useTransparentBounds(true);
// this fails to find a match! why?
System.out.println("found=" + m.find());
System.out.println("result=" + m.group());
}
}
(我在 Mac OSX Mountain Lion 上使用 J2SE 6 (1.6.0_37-b06-434-11M3909))