in reply to pattern matching

I know I should be using a module for this, but I wanna do it on my own.

Good for you. I'd rewrite:

$string = m/($start)(*)($end)/; print "$2";
as
$string =~ m/$start(.+?)$end/s; print $1;
because

Hope that helps,
andy.

Replies are listed 'Best First'.
Re: Re: pattern matching
by demerphq (Chancellor) on Jan 16, 2002 at 22:46 UTC
    As I mention above in my update the .+? is wrong. Consider
    <!-- record --> Blah 1 <!-- eorecord --> <h1>Html crap</h1> <!-- record --><!-- eorecord --> <!-- record --> Blah 2 <!-- eorecord -->
    Using the .+? we will get only two records out of the file. Using .*? we would get the correct number, three.

    Still I think its a bit funny (but not surprising) how similer our posts are... :-)

    Yves / DeMerphq
    --
    When to use Prototypes?

      Well, I guess you're right, if you'd rather have a successful (blank) pattern match than a failed match. And I guess the web page could concievably contain a blank entry, so that makes sense.

      But in the original context, we're only trying to match one record anyway, so if the match fails then $1 will contain '', and if you use .*? and the match succeeds then $1 will contain '', so I'm not sure it makes any difference. ;)

      andy.