Re: Having a problem with negative look behind assertion
by choroba (Cardinal) on Feb 12, 2013 at 12:30 UTC
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Having a problem with negative look behind assertion
by Athanasius (Archbishop) on Feb 12, 2013 at 12:41 UTC
|
#! perl
use strict;
use warnings;
while (my $line = <DATA>)
{
chomp $line;
print "$line\n" if $line =~ / [Bb]ar .* \. java $ /x &&
$line !~ / ^ fee /x;
}
__DATA__
foo/src/com/stuff/xxxxxx.java
foo/src/com/stuff/ggbargg.java
foo/src/com/stuff/ggBargg.java
fee/src/com/stuff/ggbargg.java
fee/src/com/stuff/ggBargg.java
Output:
22:32 >perl 531_SoPW.pl
foo/src/com/stuff/ggbargg.java
foo/src/com/stuff/ggBargg.java
22:37 >
Hope that helps,
Update: No need for $line:
while (<DATA>)
{
chomp;
print "$_\n" if / [Bb]ar .* \. java $ /x && !/ ^ fee /x;
}
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Having a problem with negative look behind assertion
by jethro (Monsignor) on Feb 12, 2013 at 14:08 UTC
|
I would say only Athanasius has a correct answer yet, I see mistakes in the other answers. So I made sure and tested. This should work (and still uses only one regex):
if( $line =~ /^(?!fee).*[Bb]ar.*\.java/ )
This is a negative look-ahead fixed at the start of the line (with ^).
| [reply] [Watch: Dir/Any] [d/l] |
|
I wanted a single regexp if possible and this looks like it does the trick. Many thanks for the replies !
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
If you mean to say your fourth answer is correct I beg to differ. !/fee/ aborts for "fee" anywhere in the string instead of only at the start like the OP wanted
UPDATE:
If you mean to say your third answer is correct I beg to differ. It expects a '/' after "fee". While this is always the case in the examples it is not what the OP said (although there is a chance that the OP misstated the problem)
| [reply] [Watch: Dir/Any] [d/l] |
|
Re: Having a problem with negative look behind assertion
by kcott (Archbishop) on Feb 12, 2013 at 14:10 UTC
|
$ perl -Mstrict -Mwarnings -E '
my @data = qw{
foo/src/com/stuff/xxxxxx.java
foo/src/com/stuff/ggbargg.java
foo/src/com/stuff/ggBargg.java
fee/src/com/stuff/ggbargg.java
fee/src/com/stuff/ggBargg.java
};
for (@data) {
next if /^fee/;
say if /[Bb]ar/;
}
'
foo/src/com/stuff/ggbargg.java
foo/src/com/stuff/ggBargg.java
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Having a problem with negative look behind assertion
by Anonymous Monk on Feb 12, 2013 at 12:41 UTC
|
perl -Mre=debug -E" $_=q{foo/src/com/stuff/ggbargg.java}; m/(?<!fee).*[Bb]ar.*\.java/ "
perl -Mre=debug -E" $_=q{foo/src/com/stuff/ggbargg.java}; m{(?<!fee)/.*[Bb]ar.*\.java} "
perl -Mre=debug -E" $_=q{foo/src/com/stuff/ggbargg.java}; m{^(?<!fee)/.*[Bb]ar.*\.java} "
perl -Mre=debug -E" $_=q{foo/src/com/stuff/ggbargg.java}; !/fee/ and /[Bb]ar.*\.java$/ "
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Having a problem with negative look behind assertion
by manorhce (Beadle) on Feb 12, 2013 at 13:40 UTC
|
You don't have fixed width of line, look behind or look ahead will only work on fixed width
use strict;
use warnings;
while(my $line = <DATA>)
{
chomp($line);
if( $line =~ /(?<!fee\/src\/)(com\/)/ )
{
print $line . "\n";
}
}
__DATA__
foo/src/com/
foo/src/com/
foo/src/com/
fee/src/com/
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |