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

Re: regex: extract multiple number of date patterns from certain lines

by Bloodnok (Vicar)
on Mar 04, 2009 at 15:58 UTC ( #748207=note: print w/ replies, xml ) Need Help??


in reply to regex: extract multiple number of date patterns from certain lines

while (<DATA>) { @res = /(\d{4}(?:-\d\d){2}).*dates processed: (.*)/; warn "@res"; } __DATA__ 2009-02-02 06:12:57,500 dates processed: 2009-01-31, 2009-01-29, 2009- +01-30 2009-02-18 06:03:47,713 dates processed: 2009-02-16, 2009-02-17 2009-02-19 05:58:29,138 dates processed: 2009-02-18
returns
2009-02-02 2009-01-31, 2009-01-29, 2009-01-30 at tst.pl line 3, <DATA> + line 1. 2009-02-18 2009-02-16, 2009-02-17 at tst.pl line 3, <DATA> line 2. 2009-02-19 2009-02-18 at tst.pl line 3, <DATA> line 3.

Update:

Following a change in requirmeents ;-) ...

use Data::Dumper; while (<DATA>) { @res = map { split } /(\d{4}(?:-\d\d){2}).*dates processed: (.*)/; warn Dumper \@res; } __DATA__ 2009-02-02 06:12:57,500 dates processed: 2009-01-31, 2009-01-29, 2009- +01-30 2009-02-18 06:03:47,713 dates processed: 2009-02-16, 2009-02-17 2009-02-19 05:58:29,138 dates processed: 2009-02-18
returns
$VAR1 = [ '2009-02-02', '2009-01-31,', '2009-01-29,', '2009-01-30' ]; $VAR1 = [ '2009-02-18', '2009-02-16,', '2009-02-17' ]; $VAR1 = [ '2009-02-19', '2009-02-18' ];
as required (nearly:-D) ??

A user level that continues to overstate my experience :-))


Comment on Re: regex: extract multiple number of date patterns from certain lines
Select or Download Code
Re^2: regex: extract multiple number of date patterns from certain lines
by Random_Walk (Parson) on Mar 04, 2009 at 16:08 UTC

    Nice but I did want to bust all the date values out to separate elements of the @res array. Your non capturing braces though give me the clue I think to fix it properly, but I still can't quite get it:

    @res = $_ =~/(\d{4}-\d\d-\d\d).*dates processed: ((:?\d{4}-\d\d-\d\d,? + ?)*)/ # still captures last result twice # input # 2009-02-02 06:12:57,500 dates processed: 2009-01-31, 2009-01-29, 200 +9-01-30 # output # 2009-02-02, 2009-01-31, 2009-01-29, 2009-01-30, 2009-01-30

    Update

    Oeps, I am not splitting them with the above either, fooled myself because my debug testing printed the list out with a join ", ", doh!

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://748207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-08-29 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls