filename exclusion

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mirelas75
Forum Newbie
Posts: 2
Joined: Wed Jun 13, 2007 3:35 pm

Post by mirelas75 »

Jenk wrote:Use a lookbehind or lookahead, or group your characters..

Code: Select all

/<ref>[^(ref)]*?<\/ref>/
/<ref>(?!ref).*?<\/ref>/
/<ref>.*?(?<!ref)<\/ref>/
any of those should work.
Hi,

I'm trying to write such a regular expression that shows me all filenames, except the ones with extension: pdf, xls, doc. Any idea how this regexp should look like? I recognize I am no good in writing regexp, I tried to do something like this:
'[a-zA-Z0-9\.\s_]*([^(pdf)])$'

but it shows me all files that do not end with f...

Any help would be welcome!

Thanks a lot!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Please don't attempt to hijack someone else's thread.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

I'm not sure about the input of your regex, mirelas75. Here's a quick example that should get you started.

Code: Select all

preg_match_all('/\S+\.(?!pdf|xls|doc)\w+$/m', 'file1.temp.zip
file2.pdf
file3.html
file4.doc', $matches);

$matches = Array
(
    [0] => Array
        (
            [0] => file1.temp.zip
            [1] => file3.html
        )

)
mirelas75
Forum Newbie
Posts: 2
Joined: Wed Jun 13, 2007 3:35 pm

Input and Purpose

Post by mirelas75 »

Hi,

The input to the regexp is a filename or a directory. This expression is evaluated inside a piece of software, using reg function of php, and if the filename matches this pattern that it does not gets displayed. As I want to hide all files that do not have "doc", "pdf" and "jpg" extension, my expression should match all files that have these extensions... and no other files. It should still match all directory names, though.

Is this something doable in this form, or should I try to rewrite the validation engine?

Thanks a lot!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, the regex that you posted does exactly what I'd expect it to do.

Code: Select all

[a-zA-Z0-9\.\s_]*([^(pdf)])
Your regex tries to be a subpattern in a character class, but a character class is always a character class no matter what. And actually, in the regex that you're after, you don't need any complicated lookaheads or lookbehinds because you know exactly which types you'll accept.

Code: Select all

/^[^\.]+\.(pdf|jpg|gif)$/i
Have fun with it. (You may want to make the [^\.] character class more specific, but be careful with it.)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Provided the test input is just a filename, nothing more..

Code: Select all

#\.(?:pdf|doc|whatever)$#
Remember to trim() the value before using it.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

The 'i' modifier is probably a good idea too since file extensions can be uppercased. ^_^
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That choice is up to the original poster. I normally strtolower() filenames. :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:That choice is up to the original poster. I normally strtolower() filenames. :)
Or that, or that. :P
Post Reply