note 19254 added to ref.regex
| From: | regex at dan42 dot cjb dot net | Date: | Thu, 21 Feb 2002 02:12:42 +0000 |
| Subject: | note 19254 added to ref.regex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26871@lists.php.net to get a copy of this message | ||
It's easy to exclude characters but excluding words with a regular expression is a bit more
tricky. For parentheses there is no equivalent to the ^ for brackets. The only way I've found
to exclude a string is to proceed by inverse logic: accept all the words that do NOT correspond to
the string. So if you want to accept all strings except those _begining_ with "abc",
you'd have to accept any string that matches one of the following:
^(ab[^c])
^(a[^b]c)
^(a[^b][^c])
^([^a]bc)
^([^a]b[^c])
^([^a][^b]c)
^([^a][^b][^c])
which, put together, gives the regex
^(ab[^c]|a[^b]c|a[^b][^c]|[^a]bc|[^a]b[^c]|[^a][^b]c|[^a][^b][^c])
Note that this won't work to detect the word "abc" anywhere in a string. You need to
have some way of anchoring the inverse word match
like: ^(a[^b]|[^a]b|[^a][^b]) ;"ab" not at begining of line
or: (a[^b]|[^a]b|[^a][^b])& ;"ab" not at end of line
or: 123(a[^b]|[^a]b|[^a][^b]) ;"ab" not after "123"
I don't know why "(abc){0,0}" is an invalid synthax. It would've made all this
much simpler.
Slightly off-topic, here's a regex date validator (format yyyy-mm-dd, remove all spaces and
linefeeds):
^(19|20)([0-9]{2}-((0[13-9]|1[0-2])-(0[1-9]|[12][0-9]|30)|
(0[13578]|1[02])-31|02-(0[1-9]|1[0-9]|2[0-8]))|([2468]0|
[02468][48]|[13579][26])-02-29)$
--
http://www.php.net/manual/en/ref.regex.php
http://master.php.net/manage/user-notes.php?action=edit+19254
http://master.php.net/manage/user-notes.php?action=delete+19254
http://master.php.net/manage/user-notes.php?action=reject+19254