我怎么知道在Perl正则表达式模式中哪些匹配? [英] How can I tell which of the alternatives matched in a Perl regular expression pattern?

查看:75
本文介绍了我怎么知道在Perl正则表达式模式中哪些匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式列表(大约10到15个),我需要将它们与某些文本进行匹配。逐一匹配它们太慢了。但是,我没有编写我自己的状态机来一次匹配所有正则表达式,而是尝试 | 单个正则表达式,然后让perl进行工作。问题是我如何知道哪个替代项匹配?

I have a list of regular expressions (about 10 - 15) that I needed to match against some text. Matching them one by one in a loop is too slow. But instead of writing up my own state machine to match all the regexes at once, I am trying to | the individual regexes and let perl do the work. The problem is that how do I know which of the alternatives matched?

此问题解决了每个正则表达式中没有捕获组的情况。 (其中)如果每个正则表达式内部都有捕获组,该怎么办?

This question addresses the case where there are no capturing groups inside each individual regex. (which portion is matched by regex?) What if there are capturing groups inside each regexes?

因此,下面是

/^(A(\d+))|(B(\d+))|(C(\d+))$/

和字符串 A123,我怎么都知道A123匹配并提取了 123?

and the string "A123", how can I both know that A123 matched and extract "123"?

推荐答案

您不需要编写自己的状态机来组合正则表达式。查看 Regexp:Assemble

You don't need to code up your own state machine to combine regexes. Look into Regexp:Assemble. It has methods that'll track which of your initial patterns matched.

编辑:

use strict;
use warnings;

use 5.012;

use Regexp::Assemble;

my $string = 'A123';

my $re = Regexp::Assemble->new(track => 1);
for my $pattern (qw/ A(\d+) B(\d+) C(\d+) /) {
  $re->add($pattern);
}

say $re->re; ### (?-xism:(?:A(\d+)(?{0})|B(\d+)(?{2})|C(\d+)(?{1})))
say for $re->match($string); ### A(\d+)
say for $re->capture; ### 123

这篇关于我怎么知道在Perl正则表达式模式中哪些匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆