文章目录
之前在模式序列中,我们用 begin()、next()、followedBy()、followedByAny()这样的“连接词”来组合个体模式,这些方法的参数就是一个个体模式的名称;而现在它们可以直接以一个模式序列作为参数,就将模式序列又一次连接组合起来了。这样得到的就是一个“模式组”(Groups of Patterns)。
在模式组中,每一个模式序列就被当作了某一阶段的匹配条件,返回的类型是一个GroupPattern。而 GroupPattern 本身是 Pattern 的子类;所以个体模式和组合模式能调用的方法,比如 times()、oneOrMore()、optional()之类的量词,模式组一般也是可以用的。
具体在代码中的应用如下所示:
// 以模式序列作为初始模式
Pattern <Event, ?> start = Pattern.begin(
Pattern. <Event> begin("start_start").where(...)
.followedBy("start_middle").where(...));
// 在 start 后定义严格近邻的模式序列,并重复匹配两次
Pattern <Event, ?> strict = start.next(
Pattern. <Event> begin("next_start").where(...)
.followedBy("next_middle").where(...)).times(2);
// 在 start 后定义宽松近邻的模式序列,并重复匹配一次或多次
Pattern <Event, ?> relaxed = start.followedBy(
Pattern. <Event> begin("followedby_start").where(...)
.followedBy("followedby_middle").where(...)).oneOrMore();
//在 start 后定义非确定性宽松近邻的模式序列,可以匹配一次,也可以不匹配
Pattern <Event, ?> nonDeterminRelaxed = start.followedByAny(
Pattern. <Event> begin("followedbyany_start").where(...)
.followedBy("followedbyany_middle").where(...)).optional();