One way to acheive your end, if you don't mind not knowing which pattern matched, but only need the keyword, would be to combine your patterns using the | (regex or operator) and remove the inner loop as here.

#!/usr/local/bin/perl -w use strict; my $str1 = 'ABCBXBCA'; my $str2 = 'APCBXBCAC'; my $patterns = 'B.B|CB'; foreach my $string ($str1,$str2){ # foreach my $pat (@patterns){ if($string =~ /$patterns/){ print "String:$string Pattern:$patterns KeyWor +d:$` \n";; # last; } # } } __DATA__ #ouptut C:\test>180890 String:ABCBXBCA Pattern:B.B|CB KeyWord:A String:APCBXBCAC Pattern:B.B|CB KeyWord:AP C:\test>

Update:Following jryan's post below, I thought I remembered a special var that would return the last match. Looking it up I found $+.

#!/usr/local/bin/perl -w use strict; my $str1 = 'ABCBXBCA'; my $str2 = 'APCBXBCAC'; my $patterns = 'B.B|CB'; foreach my $string ($str1,$str2){ # foreach my $pat (@patterns){ if($string =~ /($patterns)/) { print "String:$string Pattern:$+ KeyWord:$` \n +";; # last; } # } } __DATA__ #output C:\test>180890 String:ABCBXBCA Pattern:BCB KeyWord:A String:APCBXBCAC Pattern:CB KeyWord:AP C:\test>

Note the brackets in the if statement and the $+ in the print.

UpdateAs jryan notes below, this does still not completely match the requirements of the original question. I, nor anyone else it seems, had noticed that $+ returns the text matched not the pattern that matched it.

If you need to know which pattern matched, you will have to either use jryan solution from here or possibly look into using the scarey (to me at least) technique that hofmator doesn't suggest in the last if statement in this post. If you opt for the latter, you'd better takes a good look at some of japhys or abigail-II's posts as this level of regex is well beyond me at the moment.

Finally. Check perlman:perlre for dire warning regarding the performance hit of using $` which you already had, and $+ which I added.


Anyone know of an abbottoire going cheap?


In reply to Re: First Pattern Matching by BrowserUk
in thread First Pattern Matching by artist

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.