Another way (note variables  $tdate $sdate are undefined, hence easy to test, if no match exists):

>perl -wMstrict -le "my $line = 'CLASS A SOLD ON 17/11/10 FOR SETTLEMENT ON 22/11/10 @ 145.59993'; ;; my $date = qr{ \d\d / \d\d / \d\d }xms; ;; my ($tdate) = $line =~ m{ (?: PURCHASED | SOLD) \s+ ON \s+ ($date) }xmsg; print qq{tdate: '$tdate'}; ;; my ($sdate) = $line =~ m{ SETTLEMENT \s+ ON \s+ ($date) }xmsg; print qq{sdate: '$sdate'}; " tdate: '17/11/10' sdate: '22/11/10'

See also perlretut, perlrequick, perlreref.

Update: Or even (hash  %dates empty if no match):

>perl -wMstrict -le "use Data::Dumper; ;; my $date = qr{ \d\d / \d\d / \d\d }xms; my $sep = qr{ \s+ ON \s+ }xms; my @ops = qw(PURCHASED SOLD SETTLEMENT); my $op = join '|', map quotemeta, @ops; $op = qr{ $op }xms; ;; READ_LOOP: { ;; my $line = 'CLASS A SOLD ON 17/11/10 FOR SETTLEMENT ON 22/11/10 @ 145.59993'; ;; next READ_LOOP if not my %dates = $line =~ m{ ($op) $sep ($date) }xmsg; print Dumper \%dates; ;; } " $VAR1 = { 'SETTLEMENT' => '22/11/10', 'SOLD' => '17/11/10' };

In reply to Re: help with regex by AnomalousMonk
in thread help with regex by mmittiga17

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.