in reply to Regular Expression help

Perhaps you are looking for something like this instead? :)
use strict; use Data::Dumper; while (<DATA>) { chomp; # remove trailing \n, optional my @rec = split /\|/; # split records my $date = $rec[0]; my ($activity, $machine, $requester) = $rec[2] =~ /(.*)\sfor\s(.*)\s\(requested by (.*)\)/; print "$date, $activity, $machine, $requester\n"; } __DATA__ 10/1/2003 2:06:32 AM|1|Checkout Started for US-02-14@comany.com (reque +sted by aad6870) 10/2/2003 2:07:17 AM|1|Checkout Processed for US-02-14@company.com (re +quested by aad6870) 10/3/2003 2:09:37 AM|1|Checkin Processed for DN-US-02-14@company.com ( +requested by aad6870) 10/4/2003 9:37:53 AM|1|Checkout Started for DN-US-02-14@company.com (r +equested by heavis6608) 10/5/2003 9:38:29 PM|1|Checkout Processed for US-02-14@company.com (re +quested by heavis6608) 10/6/2003 10:10:21 AM|1|Checkout Started for US-02-17@company.com (req +uested by vm_karthik3521)
And the output is -
10/1/2003 2:06:32 AM, Checkout Started, US-02-14@comany.com, aad6870 10/2/2003 2:07:17 AM, Checkout Processed, US-02-14@company.com, aad687 +0 10/3/2003 2:09:37 AM, Checkin Processed, DN-US-02-14@company.com, aad6 +870 10/4/2003 9:37:53 AM, Checkout Started, DN-US-02-14@company.com, heavi +s6608 10/5/2003 9:38:29 PM, Checkout Processed, US-02-14@company.com, heavis +6608 10/6/2003 10:10:21 AM, Checkout Started, US-02-17@company.com, vm_kart +hik3521
I think the only trick here is with the my ($var) = $str =~ /(.*)/; idiom. Which is a handy one to master. tachyon had a Perl Meditation not long ago on this topic... 291543

Also you don't need to print the elements one line at a time, you can print them all at once.

Replies are listed 'Best First'.
Re: Re: Regular Expression help
by Anonymous Monk on Nov 12, 2003 at 00:54 UTC
    Hi Roger, Thanks for the help, this really deepens my understanding of regular expressions. I wonder could you tell me how I could parse it without stripping the date out first with the /\|/ split. In other words is there a way to get a record to parse without splitting?? Once again thanks for the help.
      Ok, you can change my previous code to -
      my ($activity, $machine, $requester) = /\|1\|(.*)\sfor\s(.*)\s\(reques +ted by (.*)\)/;
      I have omitted the implicit $_ =~ part in the idiom. What the new code does is to look for the |1| pattern followed by the stuff you are looking for. Note that at this point, $_ holds the entire line.