Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

help with regex

by mmittiga17 (Scribe)
on Nov 30, 2010 at 00:19 UTC ( #874400=perlquestion: print w/replies, xml ) Need Help??

mmittiga17 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

need to pull just date out of lines. Date is preceded by (PURCHASED ON|SOLD ON|SETTLED ON). example CLASS A SOLD ON 17/11/10 FOR SETTLEMENT ON 22/11/10 @ 145.59993 just want to grab 17/11/10 as $tdate and 22/11/10 as $sdate

while(defined($line = (<IN>))){ if ($line =~m/(PURCHASED ON|SOLD ON|SETTLEMENT ON)/) { $desc = (split(/\,/, $line))[27]; #print "$desc\n"; $tdate = (($desc =~ /PURCHASED\s+ON\s+(.*?)\s+/g )||($de +sc =~ /SOLD\s+ON\s+(.*?)\s+/g )); $sdate = $desc =~m/SETTLEMENT\S\s+ON\s+(.*?)\s+/g ; print "TDATE $tdate SDATE $sdate\n"; } }
Any help will be appreciated.

Replies are listed 'Best First'.
Re: help with regex
by AnomalousMonk (Archbishop) on Nov 30, 2010 at 00:56 UTC

    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' };
Re: help with regex
by toolic (Bishop) on Nov 30, 2010 at 00:36 UTC
    while (<DATA>) { if (/(PURCHASED ON|SOLD ON|SETTLEMENT ON)/) { $desc = $_; if ($desc =~ /SETTLEMENT\s+ON\s+(\S+)/) { $sdate = $1; } if ($desc =~ /(?:PURCHASED|SOLD)\s+ON\s+(\S+)/) { $tdate = $1; } print "TDATE $tdate SDATE $sdate\n"; } } __DATA__ CLASS A SOLD ON 17/11/10 FOR SETTLEMENT ON 22/11/10 @ 145.59993

    Prints:

    TDATE 17/11/10 SDATE 22/11/10

    perlre

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://874400]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2023-02-04 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (30 votes). Check out past polls.

    Notices?