Regular expression – how to determine which alternative items in the Perl regular expression mode?

I have a list of regular expressions (about 10-15) and I need to match some text. Matching them one by one in a loop is too slow. However, I did not write my own state machine To match all regular expressions at the same time, but try | personal regular expressions to let perl do the work. The question is how do I know which alternatives to match?

This problem solves the situation where there is no capture group in every regular expression. (which portion is matched by regex?) What if there is a capture group in every regular expression?

So there are the following points,

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

And the string “A123”, how can I know that A123 matches and extract “123”?

You don’t need to write your own state machine to assemble regular expressions. Check Regexp:Assemble. It There are some ways to track which initial pattern matches.

Edit:

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

I have a list of regular expressions (about 10-15) and I need to match some text. Matching them one by one in a loop is too slow. However, instead of writing my own state machine to match all regular expressions at the same time, I tried | personal regular expressions to let perl do the work. The question is how do I know which alternatives to match?

This problem solves the situation where there is no capture group in every regular expression. (which portion is matched by regex?) What if there is a capture group in every regular expression?

So there are the following points,

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

And the string “A123”, how can I know that A123 matches and extract “123”?

You don’t need to write your own state machine to assemble regular expressions. Check Regexp:Assemble. It has some ways to track which initial pattern matches.

Edit:

use strict;
use warnings;

use 5.012;
< br />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);
}< br />
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

Leave a Comment

Your email address will not be published.