There are several ways to achieve this, for example you can use named captures and the %+ hash:
use strict;
use warnings;
my $pat1 = qr/(?<pat1>field)/;
my $pat2 = qr/(?<pat2>f.i.e.l.d)/;
my $pat3 = qr/(?<pat3>the)/;
my $str = "There are many soccer fields in England - f1i2e3l4d";
while ( $str =~ m/($pat1|$pat2|$pat3)/ig ) {
print "Found '$1' from pattern ", keys %+, "\n";
}
Or you can use perl code embedded in regexes:
my $pat_id;
my $pat1 = qr/field(?{$pat_id=1})/;
my $pat2 = qr/f.i.e.l.d(?{$pat_id=2})/;
my $pat3 = qr/the(?{$pat_id=3})/;
my $str = "There are many soccer fields in England - f1i2e3l4d";
while ( $str =~ m/($pat1|$pat2|$pat3)/ig ) {
print "Found '$1' from pattern pat", $pat_id, "\n";
}
Or you can use the MARK backtracking control verb and the $REGMARK variable:
my $pat_id;
our $REGMARK;
my $pat1 = qr/field(*MARK:pat1)/;
my $pat2 = qr/f.i.e.l.d(*MARK:pat2)/;
my $pat3 = qr/the(*MARK:pat3)/;
my $str = "There are many soccer fields in England - f1i2e3l4d";
while ( $str =~ m/($pat1|$pat2|$pat3)/ig ) {
print "Found '$1' from pattern ", $REGMARK, "\n";
}
Take a look at perlre these are all documented there. For starting I would recommend you the named captures, probably those are the simplest to handle.
Hope this helps.
update: I've just noticed that I changed the meaning of your original match, because I've used precompiled subpatterns. In that case you would like to include the /i modifier in the subpattern itself: qr/the/i
|