php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24242 Match on '[' and ']' fails.
Submitted: 2003-06-18 06:05 UTC Modified: 2003-06-18 06:08 UTC
From: madsen at sjovedyr dot dk Assigned:
Status: Not a bug Package: Regexps related
PHP Version: 4.3.2 OS: Debian Linux (Woody) Unstable
Private report: No CVE-ID: None
 [2003-06-18 06:05 UTC] madsen at sjovedyr dot dk
Description:
------------
Attempting to use ereg() for matching the date from an Apache error_log fails, when using the pattern:
 "^\[([^\]]*)\]"
Intention of the pattern explained (in case I just can't see the error in it):
 ^\[      - Require first char in string to be a litteral '['.
 ([^\]]*) - Match any number of chars that is NOT ']'.
 \]       - Match a litteral ']'
When using this pattern in ereg(), like: ereg("^\[([^\]]*)\]", $string, $regs);
$regs has returned empty, when printed by a print_r();

But then if I use "^\[(.*)\]" as my pattern, I get something in $regs, not the right match, since it then runs in 'greedy mode', and catches what's between *(* and *)*:
[*(*Aug 25 2002 14:53:32] [error] [client: 88.99.111.222*)*] ...

It's really confusing me...

If this is not a bug I apologise, but I think I have taken every form of action required before reporting this as a bug.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-18 06:08 UTC] madsen at sjovedyr dot dk
Found the solution...
Appearantly '[' and ']' doesn't need to be escaped (inconsistency?), but they have to be the first characters in the list of matches....
 [2003-08-05 07:18 UTC] wiebe_j at hotmail dot com
For me this is still a problem. The next 16 codelines, logically all should display a "y". But some display a "n".

(Tried the escaped versions and the hex-versions)


echo "<br><br>1.";
echo "<br>".(preg_match("/^[\[\]]+$/","[]") ? "y" : "n" );
echo "<br>".(preg_match("/^[\]\[]+$/","[]") ? "y" : "n" );
echo "<br>".(preg_match("/^[\[\]]+$/","][") ? "y" : "n" );
echo "<br>".(preg_match("/^[\]\[]+$/","][") ? "y" : "n" );
echo "<br><br>2.";
echo "<br>".(preg_match("/^[\x5B\x5D]+$/","[]") ? "y" : "n" );
echo "<br>".(preg_match("/^[\x5D\x5B]+$/","[]") ? "y" : "n" );
echo "<br>".(preg_match("/^[\x5B\x5D]+$/","][") ? "y" : "n" );
echo "<br>".(preg_match("/^[\x5D\x5B]+$/","][") ? "y" : "n" );
echo "<br><br>3.";
echo "<br>".(ereg("^[\[\]]+$","[]") ? "y" : "n" );
echo "<br>".(ereg("^[\]\[]+$","[]") ? "y" : "n" );
echo "<br>".(ereg("^[\[\]]+$","][") ? "y" : "n" );
echo "<br>".(ereg("^[\]\[]+$","][") ? "y" : "n" );
echo "<br><br>4.";
echo "<br>".(ereg("^[\x5B\x5D]+$","[]") ? "y" : "n" );
echo "<br>".(ereg("^[\x5D\x5B]+$","[]") ? "y" : "n" );
echo "<br>".(ereg("^[\x5B\x5D]+$","][") ? "y" : "n" );
echo "<br>".(ereg("^[\x5D\x5B]+$","][") ? "y" : "n" );
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 15:01:30 2024 UTC