note 57824 added to function.eregi
| From: | JasonSmartknarlin at yahoo dot com dot au | Date: | Sat, 15 Oct 2005 05:28:40 +0000 |
| Subject: | note 57824 added to function.eregi | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97050@lists.php.net to get a copy of this message | ||
There seems to be a lot of confusion about escaping special characters within a bracket expression
enclosed in []
The reference is the regex man page at http://www.tin.org/bin/man.cgi?section=7&topic=regex
, Linked from the "Regular Expression Functions (POSIX Extended)" page in the PHP manual
at http://php.planetmirror.com/manual/en/ref.regex.php
The simple rule is there should be no backslash `\' in a bracket expression unless you are
matching it literally.
ALL special characters (the few exceptions are listed below), lose their special significance in a
bracket expression.
This includes .[$()|*+?{\ all of which have special meaning outside of a bracket expression, inside
they will match LITERALLY!
The backslash character can never escape anything in a bracket expression including itself! It is
always interpreted literally.
Here are the exceptions
^ at the start of the bracket expression will match on all but the set of characters within the
bracket expression
- (hyphen) Indicates range, so [a-z] will match a single character in the range a-z (case
insensitive for eregi).
To match a literal `-' it must be at the start or end of the bracket expression or the end of
a range, or enclosed in [. .] to be the start of a range.
Examples:
[0-9-]
[-0-9]
Both the above will match a single character from the set comprising the digits and hyphen
`-'
[!--] is the same as [!"#$%&'()*+,-]
i.e. it matches a single character from the range of characters from
!' to hyphen
-'
[[.-.]-9] is the same as [-./0123456789] or [./0123456789-]
and will match a single digit from the range of characters from hyphen `-' to digit 9
] The closing bracket indicates the end of a bracket expression but not if it immediately follows
the opening bracket [' or [^' in that case the closing bracket is matched
literally.
Examples:
[][0-9] is the same as []0-9[]
and matches a single character from the set of digits and a literal closing bracket ]'
and opening bracket ]'
[^][0-9] is the same as [^]0-9[]
and matches a single character from the group of all characters except the set of digits and
literal ]' or ['
Look at the regex man page for more on the above and the following special cases
[.characters.]
A collating element
[=characters=]
An equivalence class
[:alnum:]
A character class
That's it for special cases! ALL other characters match literally within a bracket expression
and can never be escaped with `\'
Here is a list of some of the postings here that have mistakenly tried to use special characters
within a bracket expression.
28-Jan-2002 12:56, 09-Feb-2002 06:28, 24-Sep-2002 01:21, 08-Mar-2003 02:21, 11-Apr-2003 03:22 (uses
|' as if it was or' it is not, it is a literal `|'), 27-Jun-2003 04:03,
06-Sep-2003 07:17, 20-Apr-2004 04:55, 24-Nov-2004 09:12, 25-Nov-2004 02:11, 06-Jan-2005 08:54,
24-Feb-2005 06:05
It seems people are copying each others mistakes so this needs to be cleared up. Most of the
mistakes attempt to escape special characters within a bracket expression with backslash. That never
works! A simple fix is to simply remove any backslashes within the bracket expression that are not
meant to be matched literally.
The attempt to use | as 'or' within a bracketed expression should be put in parentheses
instead.
I used a simple script attached to a form to test these results. Here's the script.
<?php
if(isset($_POST['pattern']) && isset($_POST['teststring'])) {
$pattern = stripslashes($_POST['pattern']);
$teststring = stripslashes($_POST['teststring']);
$match = ereg($pattern, $teststring, $regs);
if(!$match) $match = 0;
echo('<p>The regular expression is: '.$pattern.'</p>');
echo('<p>The string for testing is: '.$teststring.'</p>');
echo('<p>The number of matching characters is: '.$match.'</p>');
}
?>
Just use a form with input fields named "pattern" and "teststring"
Hope this helps and clears up some misconceptions
Jason Smart
Monash University Student
----
Manual Page -- http://www.php.net/manual/en/function.eregi.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57824
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57824&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57824&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57824&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57824&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57824&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57824&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57824&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57824&report=yes
Search -- http://master.php.net/manage/user-notes.php