Perl regex expression to return values

I

ian

Cut un paste and you will see the problem
Code comment shows what I can't do !!
NOTE! DATA is 2 (TWO) lines only.

#!/usr/bin/perl
my $n = "\n";
while ( <DATA> ) {
chomp;
my ( @bu ) = $_ =~ m{
.*
(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d)
.\d*\s
([\w]*)[\W]
(.*-\s*)
(Returning\sfrom|Entering)\s* #
Should be able to get 1 or n words
([\w._]+)
# Problem Area !!
(?:
(\(.*\))
|
(\(.*\))
(,\s+[\w]+:\s+)
(.*)
)
}x;
my $c = 0;
for my $q ( @bu ) {
$c++;
printf "%2s '%s'\n", $c, $q;
}
print $n;
next;
}
print "-----------".$n;


# RETURNS:-
#
# 1 '2009-09-01 15:58:51'
# 2 'INFO'
# 3 ' [ com.manager ] - '
# 4 'Entering'
# 5 'get.Prod_Codes'
# 6 '( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, instance:TEST)'
# 7 ''
# 8 ''
# 9 ''
#
# 1 '2009-09-01 15:58:51'
# 2 'INFO'
# 3 ' [ com.manager ] - '
# 4 'Returning from'
# 5 'get.Prod_Codes'
# 6 '( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, instance:TEST),
result: id:017661, productCodes: [GEAR | BOX | TARGET (2000)]
, Descn: [Mechan | Type 1 (2000)'
# 7 ''
# 8 ''
# 9 ''
#
#-----------


# BUT WANT the return from to look like:-
# . . . . . .
# 6 '( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, instance:TEST)
# 7 ', result: '
# 8 '(id:017661, productCodes: [GEAR | BOX | TARGET (2000)], Descn:
[Mechan | HARDENED | Type 1 (2010) ])'
#

__DATA__
Server.log:2009-09-01 15:58:51,513 INFO [ com.manager ] - Entering
get.Prod_Codes( id:017661, date: Tue Sep 01 15:58:51 CEST 2009,
instance:TEST)
Server.log:2009-09-01 15:58:51,531 INFO [ com.manager ] - Returning
from get.Prod_Codes( id:017661, date: Tue Sep 01 15:58:51 CEST
2009, instance:TEST), result: id:017661, productCodes: [GEAR | BOX |
TARGET (2000)], Descn: [Mechan | HARDENED | Type 1 (2010) ]
)
 
S

sln

Cut un paste and you will see the problem
Code comment shows what I can't do !!
NOTE! DATA is 2 (TWO) lines only.

#!/usr/bin/perl
use strict;
use warnings;
my $n = "\n";
while ( <DATA> ) {
chomp;
my ( @bu ) = $_ =~ m{
.*
(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d)
.\d*\s
([\w]*)[\W]
(.*-\s*)
(Returning\sfrom|Entering)\s*
([\w._]+)
(\(.*?\))
(,\s+[\w]+:\s+|)
(.*|)

}x;
my $c = 0;
for my $q ( @bu ) {
$c++;
printf "%2s '%s'\n", $c, $q;
}
print $n;
next;
}
print "-----------".$n;

-sln


1 '2009-09-01 15:58:51'
2 'INFO'
3 ' [ com.manager ] - '
4 'Entering'
5 'get.Prod_Codes'
6 '( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, instance:TEST)'
7 ''
8 ''

1 '2009-09-01 15:58:51'
2 'INFO'
3 ' [ com.manager ] - '
4 'Returning from'
5 'get.Prod_Codes'
6 '( id:017661, date: Tue Sep 01 15:58:51 CEST 2009, instance:TEST)'
7 ', result: '
8 'id:017661, productCodes: [GEAR | BOX | TARGET (2000)], Descn: [Mechan | HAR
DENED | Type 1 (2010) ])'

-----------
 
J

John W. Krahn

ian said:
Cut un paste and you will see the problem
Code comment shows what I can't do !!
NOTE! DATA is 2 (TWO) lines only.

#!/usr/bin/perl
my $n = "\n";
while ( <DATA> ) {
chomp;
my ( @bu ) = $_ =~ m{
.*
(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d)
.\d*\s
([\w]*)[\W]
(.*-\s*)
(Returning\sfrom|Entering)\s* #
Should be able to get 1 or n words
([\w._]+)
# Problem Area !!
(?:
(\(.*\))
|
(\(.*\))
(,\s+[\w]+:\s+)
(.*)
)
}x;
my $c = 0;
for my $q ( @bu ) {
$c++;
printf "%2s '%s'\n", $c, $q;
}
print $n;
next;
}
print "-----------".$n;

#!/usr/bin/perl
use warnings;
use strict;

while ( <DATA> ) {
chomp;
my @bu = m{
.*
( \d\d\d\d - \d\d - \d\d \s+ \d\d : \d\d : \d\d )
. \d* \s
( \w* ) \W
( .* - \s* )
( Returning \s+ from | Entering ) \s* # Should be
able to get 1 or n words
( [\w._]+ )
( \( [^()]* \) )
(?:
( , \s+ \w+ : \s+ )
( .* )
)?
}x;

my $c;
for my $q ( @bu ) {
next unless defined $q;
printf "%2s '%s'\n", ++$c, $q;
}
print "\n";
next;
}
print "-----------\n";

__DATA__
Server.log:2009-09-01 15:58:51,513 INFO [ com.manager ] - Entering
get.Prod_Codes( id:017661, date: Tue Sep 01 15:58:51 CEST 2009,
instance:TEST)
Server.log:2009-09-01 15:58:51,531 INFO [ com.manager ] - Returning
from get.Prod_Codes( id:017661, date: Tue Sep 01 15:58:51 CEST 2009,
instance:TEST), result: (id:017661, productCodes: [GEAR | BOX | TARGET
(2000)], Descn: [Mechan | HARDENED | Type 1 (2010) ])




John
 
I

ian

Netherlands works, but John's does not !!
To be fair to John, this may come down to
version, as the Perl blardy blar tells us this is
very Release concious.
I am using Perl 5 version 10.

I can't help but think that Perl missed a golden
opportunity here in sort of having a Perl
function sub set inside the m{...}x;

EG:-
my $success = m{
.*
my $date = ( \d{4] - \d{2} - ....... )
.*
my $srv = ( \w* ) \W
.*
... etc
}x;


Regards
Ian
 
S

sln

Netherlands works, but John's does not !!
To be fair to John, this may come down to
version, as the Perl blardy blar tells us this is
very Release concious.
I am using Perl 5 version 10.

I can't help but think that Perl missed a golden
opportunity here in sort of having a Perl
function sub set inside the m{...}x;

EG:-
my $success = m{
.*
my $date = ( \d{4] - \d{2} - ....... )
.*
my $srv = ( \w* ) \W
.*
... etc
}x;


Regards
Ian

John's code works on my machine. Maybe something got lost in the cut'n paste.

As far as asigning specific capture to variables, that is possible
in perl 5.8 or 5.10

Inside a code block (?{..}) (version 5.8 and up):
( Returning \s+ from | Entering ) \s* (?{ $Action = $^N })

There's always named capture variables (new in 5.10):
-------------
use strict;
use warnings;

while ( <DATA> ) {
chomp;
m{
.*
(?<O1_Date> \d\d\d\d - \d\d - \d\d \s+ \d\d : \d\d : \d\d )
. \d* \s
(?<O1_Type> \w* ) \W
(?<O2_From> .* - \s* )
(?<O3_Action> Returning \s+ from | Entering ) \s*
(?<O4_Function> [\w._]+ )
(?<O5_Parms> \( [^()]* \) )
(?:
(?<O6_Result> , \s+ \w+ : \s+ )
(?<O7_Output> .* )
)?
}x;

my %capt = %+;
for my $key (sort keys %capt) {
my $val = $capt{$key};
$key =~ s/.*_//;
print "$key: \t$val\n";
}
print "\n";
next;
}
print "-----------\n";
__END__

-sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top