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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5440 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.