in reply to Problem with CGI script not working (regex at fault)

"why does my regex simply not match, return false, the if not execute, and the loop continue?"--

First of all, the code is working like that, because that is what you have it programmed to do. Your regular expression is failing. When a regex fails it return a false value which you if is evaluating, seeing is obviously not true, and not executing its conditional block.

So, the tricky part is, why does your regex not match? Well, there are a couple of things. For one, you are using the '.*?' construct quite a bit. You mentioned the Death to Dot Star! node, but the construct you are using is different than .* is a very imortant way. The question mark makes the .*, non--greedy...matching as little as it can. It looks like you wanted to use the greedy nature of .* to your advantage, but you added the question mark, changing its nature.

Watch what happens with these two examples:

$str = "<tr><td width="0" align="center"><font face="Arial" size="2">5 + Digits </font></td>"; if ($str =~ m/(<[Tt][Rr].*>)/ ) { # using .* print $1, "\n"; } # or $str = "<tr><td width="0" align="center"><font face="Arial" size="2">5 + Digits </font></td>"; if ($str =~ m/(<[Tt][Rr].*?>)/ ) { # using .*? print $1, "\n"; }
The regexes in both example will succeed, but the output will be very different.

One more thing: You are saying while (<FH>), which is all well and good, but your regex is testing the entire contents of the table. Unless that table is all on one line of the file your regex has way too much in it.

Now, knowing what you now know about greediness and non-greediness, go back and tweak your regex.

Amel - f.k.a. - kel

Replies are listed 'Best First'.
Re: Re: Problem with CGI script not working (regex at fault)
by deryni (Beadle) on Jul 30, 2001 at 01:58 UTC
    I was perhaps unclear in what I wanted to happen. I wanted the regex to fail on the lines it did not match, then return a false value to be evaluated by the if, have the if evaluate to false and skip it's contents, which would then allow the while loop to continue onto it's next iteration.
    Each line of the input file does indeed consist of one (except in the special cases mentioned in my response to tachyon when it consists of two) table row(s).

    Thank you for being an ever vigilant watchdog. The perlmonks community is well served by those who keep such careful, and coureous, watch over it's supplicants.

       -Etan