Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl regex expression to return values

Reply
Thread Tools

Perl regex expression to return values

 
 
ian
Guest
Posts: n/a
 
      09-08-2009
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) ]
)
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      09-08-2009
On Tue, 8 Sep 2009 10:04:34 -0700 (PDT), ian <(E-Mail Removed)> wrote:

>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) ])'

-----------


 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-08-2009
ian wrote:
> 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
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
ian
Guest
Posts: n/a
 
      09-09-2009
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
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      09-09-2009
On Wed, 9 Sep 2009 01:27:13 -0700 (PDT), ian <(E-Mail Removed)> wrote:

>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments