CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Lookahead doesn't work

 



damluar
New User

Jan 9, 2010, 11:55 PM

Post #1 of 7 (4506 views)
Lookahead doesn't work Can't Post

Hello everyone!
Pls help me out with lookahead. I have strings like "Paris ... 5/5 ..."
I need to find strings that start with "Paris" and don't contain "1/5" and "5/5". I tried this expression:


Code
 $x =~ /\AParis.*(?!1\/5).*/


but it matchs the string above... Can anybody help?


shawnhcorey
Enthusiast


Jan 10, 2010, 5:58 AM

Post #2 of 7 (4504 views)
Re: [damluar] Lookahead doesn't work [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;

while( my $x = <DATA> ){
if( $x !~ /\AParis.*(?=[15]\/5).*/ ){
print $x;
}
}

__DATA__
Paris ... 0/5 ...
Paris ... 1/5 ...
Paris ... 2/5 ...
Paris ... 3/5 ...
Paris ... 4/5 ...
Paris ... 5/5 ...
Paris ... 6/5 ...
Paris ... 7/5 ...
Paris ... 8/5 ...
Paris ... 9/5 ...


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


damluar
New User

Jan 10, 2010, 7:57 AM

Post #3 of 7 (4502 views)
Re: [shawnhcorey] Lookahead doesn't work [In reply to] Can't Post

Thank you, but I told about negative lookahead, not positive one!


shawnhcorey
Enthusiast


Jan 10, 2010, 8:07 AM

Post #4 of 7 (4499 views)
Re: [damluar] Lookahead doesn't work [In reply to] Can't Post

Negative lookahead matches if something found does not match. If you have

$x = 'Paris ... 1/5 ...';
$x =~ /\AParis.*(?!1\/5).*/

Then the ' ...' after Paris matches the negative lookahead since a space and three periods does not match 1/5

Negative lookaheads generally do not work if what's before it can match zero times. You need something more definite.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


damluar
New User

Jan 10, 2010, 8:26 AM

Post #5 of 7 (4497 views)
Re: [shawnhcorey] Lookahead doesn't work [In reply to] Can't Post

Thank you for your help. I need to find only strings that contain Paris at the beginning and do not contain 1/5 and 5/5.
I made it this way:
if (/\AParis/){
if ($_ !~ /(5\/5|1\/5)/){
$line_counter++;
print $_, "\n";
};
}

but how can I combine this 2 expressions? I thought that I could use negative lookahead...


(This post was edited by damluar on Jan 10, 2010, 8:26 AM)


shawnhcorey
Enthusiast


Jan 10, 2010, 8:43 AM

Post #6 of 7 (4495 views)
Re: [damluar] Lookahead doesn't work [In reply to] Can't Post


In Reply To
but how can I combine this 2 expressions? I thought that I could use negative lookahead...


Why? Anyone who has to maintains your code can read what's it's doing as is. Why make things complicated?

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


damluar
New User

Jan 10, 2010, 9:40 AM

Post #7 of 7 (4491 views)
Re: [shawnhcorey] Lookahead doesn't work [In reply to] Can't Post

It's a rhetorical question ))) Thank you for your help!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives