Page 1 of 1
Posted: Wed Jun 13, 2007 3:42 pm
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!
Posted: Wed Jun 13, 2007 8:39 pm
by feyd
Please don't attempt to hijack someone else's thread.
Posted: Thu Jun 14, 2007 11:49 am
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
)
)
Input and Purpose
Posted: Sun Jun 17, 2007 6:33 am
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!
Posted: Sun Jun 17, 2007 7:44 am
by superdezign
Well, the regex that you posted does exactly what I'd expect it to do.
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.
Have fun with it. (You may want to make the [^\.] character class more specific, but be careful with it.)
Posted: Sun Jun 17, 2007 7:49 am
by feyd
Provided the test input is just a filename, nothing more..
Remember to
trim() the value before using it.
Posted: Sun Jun 17, 2007 7:55 am
by superdezign
The 'i' modifier is probably a good idea too since file extensions can be uppercased. ^_^
Posted: Sun Jun 17, 2007 7:57 am
by feyd
That choice is up to the original poster. I normally
strtolower() filenames.

Posted: Sun Jun 17, 2007 8:04 am
by superdezign
feyd wrote:That choice is up to the original poster. I normally
strtolower() filenames.

Or that, or that.
