Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

HI Monks,
I am trying to use regular expressions to parse this string:
my $test = "Auto And Company 57 DI 13 FA 13 FAI 4 Login 23 Not Found 4 +"; if($d_log=~/(.*?)\s(\d+)\s(\w+)\s(\d+)\s(\w+)\s(\d+)\s(\w+)\s(\d+)\s(\ +w+)\s(\d+)\s(\w+)\s(\w+)\s(\d+)/gi) print $1; }

Why isn't my regular expression working, I can't see the problem anymore!
Any help please???

Replies are listed 'Best First'.
Re: Regular Exp. Problem!
by kvale (Monsignor) on Apr 22, 2004 at 04:39 UTC
    You are matching $d_log, not $test, and are missing a brace:
    my $d_log = "Auto And Company 57 DI 13 FA 13 FAI 4 Login 23 Not Found +4"; if( $d_log =~ /^ (.*?)\s (\d+)\s (\w+)\s (\d+)\s (\w+)\s (\d+)\s (\w+)\s (\d+)\s (\w+)\s (\d+)\s (\w+)\s (\w+)\s (\d+) $ /x) { print $1; }
    Consider using the //x modifier for long complex regexps; it really helps readability.

    -Mark

Re: Regular Exp. Problem!
by perlinux (Deacon) on Apr 22, 2004 at 10:02 UTC
    Two errors:

    $test or $d_log???? (simple!)
    { not open after the if

    and magically it works! :-)

    again, try this:
    my $test = "Auto And Company 57 DI 13 FA 13 FAI 4 Login 23 Not Found 4 + "; if($test=~/(.*?)(\s\d+\s\w+){5}\s(\w+)\s(\d+)/gi) { print $1; }
    more sintetic regex...
Re: Regular Exp. Problem!
by dragonchild (Archbishop) on Apr 22, 2004 at 12:21 UTC
    Why are you using a regex? Why don't you use my @arr = split $test; That's what you're essentially doing in your regex, anyways.

    You can then recombine certain portions, such as "Auto And Company", by pulling off the items you know should be in certain places, from the right side of the string, not the left.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Regular Exp. Problem!
by Gerard (Pilgrim) on Apr 22, 2004 at 06:37 UTC
    Because you made a mistake. Try here beacuse I don't know what you are trying to do exactly.
Re: Regular Exp. Problem!
by Belgarion (Chaplain) on Apr 22, 2004 at 15:56 UTC

    You should probably look at the following thread since your input looks amazingly similar: Parsing String