Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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


In reply to Re: Determining which pattern matched by rubasov
in thread Determining which pattern matched by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (19)
    As of 2015-07-29 16:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (265 votes), past polls