Re: QF - regex rule
| From: | Florent Monnier | Date: | Wed, 12 May 2004 06:02:36 +0000 |
| Subject: | Re: QF - regex rule | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-12428@lists.php.net to get a copy of this message | ||
> I am starting to hate phps regex more and more ... Perl was much more
> clear to me ...
It seems you did not tryed to make your problem in Perl neither :p
> Maybe I am wrong but to me "Perl style" seems to be not really true ...
The PHP preg_*() functions use strict Perl regexs.
(You can find very tricky small differences in the pcre documention,
but these differences are so small that we can consider it 99.9% conformant)
So if your regex does not work in a PHP preg_*() func, it won't work in Perl
neither.
So if you are not a regex master yet, just learn from:
man perlrequick (for beginers)
man perlre (full exhaustive documentation)
Those documentations are from perldoc but works the same with PHP preg_*
functions as I said.
If you don't use a system with mans, you'll find those on the web or in your
perldocs.
http://www.perlpod.com/5.9.1/pod/perlrequick.html
http://www.perlpod.com/5.9.1/pod/perlre.html
(I am not a monger at all, but I always read those mans for details.)
There are a lot of tutorials on the web too, to learn about Perl
regex syntax.
> I am trying to use the regex rule to exclude forbidden characters of
> filenames in a textbox. The forbidden characters (Windows) are:
> "\/:*?"<>" so I tried something like:
>
> [^\/:*?"<>] first and then {[^\/:*?"<>]}, {.*[^\/:*?"<>].*}
> and a lot
> stuff like that but the result is always that the rule only works for a
> single forbidden character.
OK, your problem is quite simple: excluding some characters:
\ / : * ? " < >
just start to escape the characters that have a regex meaning:
* ? become \* \?
Put the regex inside single quote string, or escape the " too.
For the regex separator don't choose one that is inside so use | or ! for
exemple.
You also want to esclude an escape char, in PHP to put an escape char in a
string, you have to escape it, in a perl regex it also has to be escaped,
so this will be two double escapes like this \\\\ (pcre will only see \\)
if (preg_match('![\\\\/:\*\?"<>]!', $name))
echo "$name is a Wrong Name!\n";
else
echo "$name is a Good Name!\n"
--
Cheers