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

Hi Monks

I have doubt in negative lookahead assertion.

<sec>here the text goes <pg> and text continues

I want to fetch the text from <sec> tag to <pg> tag, but the text fetched should not have <pg> tag. I used the following coding for the above text. is this correct? If not please clarify my doubts.

$str =~ /<sec>.*(?!<pg>)<pg>/si;

Also i need to study the negative and positive look ahead and look behind assertions, where can i find the study material with good examples

Thanks in advance

Replies are listed 'Best First'.
Re: negative look ahead
by delirium (Chaplain) on Jun 30, 2004 at 01:25 UTC
    You can do this without an assertion by making the regex not greedy:

    ($match) = $str =~ /<sec>(.*?)<pg>/;

    Try perldoc.com and look at the POD "perlre" for some information about lookaheads and behinds. They're neat, but a lot of times they can be avoided by rethinking what you're searching for.

Re: negative look ahead
by ercparker (Hermit) on Jun 30, 2004 at 04:32 UTC
    if you want to use look ahead assertion you can do it like this
    with positive look ahead assertion:
    my $string = qq*<sec>here the text goes <pg> and text continues*; my ($match) = $string =~ /(<sec>.*)(?=<pg>)/;
    which will match from the <sec> up to the <pg>
    look at perldoc perlre for some great documentation