Regex question - identify which pattern comes first
2 direct replies — Read more / Contribute
|
by harangzsolt33
on May 01, 2026 at 20:12
|
I feel like I have learned a lot in the past 10 years since I started learning Perl, but I still probably don't know more than half of what's possible using regex. I have often come across situations where I needed to identify which pattern occurs first in a string. So, I am not trying to capture a part of the pattern nor am I trying to identify if it occurs at all or where. I am just trying to figure our which of the possible patterns is FIRST in the string. For example:
Sample string: "AB ABDA DCACCB AAA BSAA CAAB ACS ABA DBA BA DASSABACA
+A"
I'm looking for either: BA[ABC]{2} OR CA[CD]{2} OR DA[SC]{2}
So, I would write: /BA[ABC]{2}|CA[CD]{2}|DA[SC]{2}/
Is there a way to get a return value of 1, 2, or 3 depending on which pattern was matched first? How would I do that?
|
I am attempting to use a Path::Iterator::Rule "Custom rule subroutine".
I have a regexp during the buildup of my rules, which determines whether the custom rule subroutine is used. If it indicates to use the subroutine, then its capture group $1 at this point holds some text that I want to match in the subroutine to filter for certain pathnames.
With the naive approach, it seems that the $1 or $var variable in my regexp is only expanded at the time of the regexp/subroutine being run. At this point, the variable is no longer valid and could have been overwritten by some other value. Naive code:
my $rule = Path::Iterator::Rule->new;
if ($id =~ /^([0-9A-F]{2})/) {
my $int = $1;
$rule->and( sub { m#/INT $int# } );
}
Resulting errors are many times this line:
Use of uninitialized value $int in regexp compilation at [redacted]/proj/intlist/intlist.pl line 1167.
I found that it appears to work if I use a "postponed" regular subexpression like so:
my $rule = Path::Iterator::Rule->new;
if ($id =~ /^([0-9A-F]{2})/) {
my $int = $1;
$rule->and( sub { m#/INT (??{ "$int" })# } );
}
Is this a correct approach? Are there other ways to interpolate a variable at the time of adding the custom rule subroutine, so that the rule doesn't refer to the variable later but rather uses the text that it used to hold?
|