PCRE to filter files by extension

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

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

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

I know that this can be done using negation, as shown below, but I was looking for something without using negation.

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

Also, please give a brief description of how your Regex works, if possible.

early

+3
source share
1 answer

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

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

It matches the end of the line, but only if it is not preceded by .png, .jpg or .tiff.

Perl, , :

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

Source: https://habr.com/ru/post/1726963/


All Articles