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

Hey monks
What sort of Regex would match a line of this type

--,2.000000e+0,6.460000e+0,--,--,--

The commas are used as seperators. This is a result from a SQL statement. I want to find all results with the first and last three columns empty. The results will always have 6 columns. The second and third column will always take the same form i.e. 1.920000e+2

--,--,--,--,1.920000e+2,1.017000e+1 --,--,--,--,2.560000e+2,1.007000e+1 --,2.000000e+0,6.500000e+0,--,--,-- --,2.000000e+0,6.480000e+0,--,--,--

I need a regex that will match the last two but not the first two.

j o h n i r l .

Sum day soon I'Il lern how 2 spelI (nad tYpe)

Replies are listed 'Best First'.
Re: Matching Stuff with Regex
by Abigail-II (Bishop) on Aug 19, 2002 at 13:12 UTC
Re: Matching Stuff with Regex
by talexb (Chancellor) on Aug 19, 2002 at 13:13 UTC
    I would recommend using split to help you out rather than trying to do the whole thing using a regex.
    # Assuming that the comma delimited stuff is in $_ my @Values = split ( ",", $_ ); my $Match = ( $Values[0] eq $Null && $Values[3] eq $Null && $Values[4] eq $Null && $Values[5] eq $Null );

    --t. alex

    "Mud, mud, glorious mud. Nothing quite like it for cooling the blood!"
    --Michael Flanders and Donald Swann

Re: Matching Stuff with Regex
by tommyw (Hermit) on Aug 19, 2002 at 13:10 UTC

    /^--,(.*?),(.*?),--,--,--$/

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

Re: Matching Stuff with Regex
by hotshot (Prior) on Aug 19, 2002 at 13:17 UTC
    you can try:
    while ($line = <INPUT>) { # or where ever you get your input from @temp = split()/,+/, $line); if ($temp[0] eq '--' && $temp[3] eq '--' && $temp[4] eq '--' && $temp[5] eq '--') { # then do whatever you want } }
    or you can try:
    while ($line = <INPUT>) { /([e\d+.]+),[the same],[the same],([the same]),([the same]),([the sa +me])/; if ($1 eq '--' || $2 eq '--' || $3 eq '--' && $4 eq '--') { # do your ting } }


    Hotshot
Re: Matching Stuff with Regex
by Silicon Cactus (Scribe) on Aug 19, 2002 at 19:07 UTC
    If you only want to get data with the specific columns empty, why not just build your SQL query to only pull back the results you want? I would think that would be more efficient than pulling all of the values from the DB, and THEN filtering.

    Let the DB do the work it was built for. :)
      That's a good point but I'm taking the results found by the SQL statement and reordering them in a weird way. Thats why I'm looking for them. So I'm not deleting them just moving them.
      Good suggestion though

      j o h n i r l .

      Sum day soon I'Il lern how 2 spelI (nad tYpe)