Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Negative Look Ahead issue

by prasadbabu (Prior)
on Jul 28, 2008 at 12:31 UTC ( #700510=perlquestion: print w/ replies, xml ) Need Help??
prasadbabu has asked for the wisdom of the Perl Monks concerning the following question:

Hi Fellow Monks,

Good Day.
A few minutes back while coding I faced the below problem.

I was trying to match all the string till it matches two consecutive new line character. Usually I use negative look ahead format for matching such kind of string. But today I was not able to match the string and I got the below error message since I used 'warnings'.

May I know the reason why I am getting the error, can't I use newline characters for these kind of matching?

The below code and data is sample for testing, not real code and data.

use strict; use warnings; my $s = do {local $/, <DATA>}; $s =~ s/\w+\s+\w+\s*\d+\s*\,(?:(?!(?:\n\n).)*)\n\n/'test'/egsi; #getti +ng error #$s =~ s/\w+\s+\w+\s*\d+\s*\,.*?\n\n/'test'/egsi; #working fine print $s; output: ------- (?!(?:\n\n).)* matches null string many times in regex; marked by <-- +HERE in m/ \w+\s+\w+\s*\d+\s*,(?:(?!(?:\n\n).)* <-- HERE )\n\n/ at pract.pl line +6. LMTRILOG db 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 +, 0 , 0 , 0; db 25 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 + , 0 , 0 , 0; __DATA__ LMTRILOG db 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 +, 0 , 0 , 0; db 25 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 + , 0 , 0 , 0;
updated: added __DATA__

Thanks in advance

Prasad

Comment on Negative Look Ahead issue
Download Code
Re: Negative Look Ahead issue
by moritz (Cardinal) on Jul 28, 2008 at 12:38 UTC
    (?!(?:\n\n).) matches the empty string, and doing that many times (*) doesn't make sense.

    You probably want (?:(?!\n\n).)* instead.

    But it's hard to tell without seeing the expected result.

    my $s = do {local $/, <DATA>};

    That's pretty useless since you haven't defined any __DATA__ section.

Re: Negative Look Ahead issue
by olus (Curate) on Jul 28, 2008 at 12:44 UTC

    This prints test. Changed the lookahead to ((?:(?!\n\n).)*)

    use strict; use warnings; my $s = do {local $/, <DATA>}; $s =~ s/\w+\s+\w+\s*\d+\s*\,((?:(?!\n\n).)*)\n\n/'test'/egsi; print $s; __DATA__ LMTRILOG db 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 +, 0 , 0 , 0; db 25 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 + , 0 , 0 , 0;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://700510]
Approved by olus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-11-26 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (160 votes), past polls