G'day onlyauto,

Welcome to the monastery.

I see you have a solution to your posted problem. There's another issue I thought I'd point out.

All those if statements:

Consider writing your code more along these lines:

#!/usr/bin/env perl -l use strict; use warnings; my @data = ('X_12_yzAAA', 'X_34_yzBBB', 'X_56_yzCCC', 'X_78_yzAAA',); my $re = qr{^.+_(\d+)_.+([A-C]{3})$}; my %despatch = ( AAA => sub { print 'found1 with digits: ', shift }, BBB => sub { print 'found2 with digits: ', shift }, CCC => sub { print 'found3 with digits: ', shift }, ); /$re/ && $despatch{$2}->($1) for @data;

Output:

found1 with digits: 12 found2 with digits: 34 found3 with digits: 56 found1 with digits: 78

Now you have:

-- Ken


In reply to Re: regex problem: doesn't work on the first search but works on the second by kcott
in thread regex problem: doesn't work on the first search but works on the second by onlyauto

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.