tags:

views:

177

answers:

1

Hi,

I need a PCRE(Perl Compatible Regular Expression) that will match all non-images(jpg,png,tiff) from a list of files. So I need something go in place of XXX

# Perl
while(<>){
chomp;
if(/XXX/){
// non-image
}
}

// PHP
foreach($files as $file){    
if(preg_match('/XXX/',$file){
// non-image
}
}

I know it can be done using negation like below, but I was looking for something without using negation.

if(!/\.jpg$/)
{
}

Also please provide a brief explanation of how your Regex work, if possible

thanks in advance

+5  A: 

Here's a solution using a negative lookbehind (?<! ... ):

/(?<!\.png|\.jpg|\.tiff)$/

It matches the end of the string, but only if it isn't preceded by .png, .jpg or .tiff.

Here's a Perl friendly version using only fixed width look behinds:

/(?<!\.png)(?<!\.jpg)(?<!\.tiff)$/
Mark Byers
@Mark: I get this error: Variable length lookbehind not implemented in regex; marked by <-- HERE in m/(?<!\.png|\.jpg|\.tiff)$ <-- HERE / at a.pl line 4.
gameover
Perhaps Perl can't handle it, but it works fine in PHP where I tested it. Variable length lookbehinds in general are not well supported by many regex implementations and you should avoid using them if possible.
Mark Byers
I've made a version that works in Perl too. Instead of one variable width lookbehind, it uses three fixed with lookbehinds, all of which must fail to match for the regex match to succeed.
Mark Byers
Perfect!! :) Thanks a lot.
gameover
You're welcome.
Mark Byers
`glob()` is nice too
Kristopher Ives