Perl regex expression to return values

Discussion in 'Perl Misc' started by ian, Sep 8, 2009.

  1. ian

    ian Guest

    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) ]
    )
    ian, Sep 8, 2009
    #1
    1. Advertising

  2. ian

    Guest

    On Tue, 8 Sep 2009 10:04:34 -0700 (PDT), 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

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

    -----------
    , Sep 8, 2009
    #2
    1. Advertising

  3. 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
    John W. Krahn, Sep 8, 2009
    #3
  4. ian

    ian Guest

    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
    ian, Sep 9, 2009
    #4
  5. ian

    Guest

    On Wed, 9 Sep 2009 01:27:13 -0700 (PDT), ian <> 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
    , Sep 9, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,604
    Ant...
    Nov 6, 2003
  2. Replies:
    2
    Views:
    589
  3. Greenhorn
    Replies:
    15
    Views:
    795
    Keith Thompson
    Mar 6, 2005
  4. Replies:
    3
    Views:
    726
    Reedick, Andrew
    Jul 1, 2008
  5. Alont
    Replies:
    17
    Views:
    186
    Joe Smith
    Sep 27, 2004
Loading...

Share This Page