Re: Ereg bug?
| From: | Lars Torben Wilson | Date: | Mon, 04 Sep 2000 18:27:11 +0000 |
| Subject: | Re: Ereg bug? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-15168@lists.php.net to get a copy of this message | ||
Lee Willis writes:
> I'm having problems with some eregs/pregs in PHP 4.0.1p2 and I wanted to
> post them here to check I wasn't being dumb before reporting this as a
> bug.
[trim]
> Another more complicated example (Which is closer to what I was trying
> to do ...) is:
>
>
> <?
>
> if (ereg ('^[^\]\{(.*)[^\]\}', '{abc}', $matches)) {
> while (list ($key, $value) = each ($matches)) {
> echo "$key = $value<BR>";
> }
> } else {
> echo "REGEX didn't match.";
> }
>
> ?>
>
> Which I think should match a {} enclosed string as long as there is no \
> before both the { and }
>
> It doesn't. These *seem* to be bugs in PHP/regex library but I'd be
> grateful if anyone can check them over and check it's not just me being
> dumb, and maybe even explain where I'm going wrong ...
>
> Thanks
> Lee
This doesn't look like a bug to me: the regex isn't matching because
it's looking for a non-slash character before the { and }; there is no
provision for there *not* to be a slash there. Throwing in a
'zero-or-more' (or 'zero-or-one') modifier fixes it; below I use a
'?':
<?php
error_reporting(E_ALL);
$tests = array ('{abc}', '\{abc}', '\{abc\}');
while (list(, $test) = each($tests)) {
echo "Testing '$test': ";
if (ereg ('^[^\]?\{(.*)[^\]?\}', $test, $matches)) {
echo "Got '$matches[1]'.\n";
} else {
echo "REGEX didn't match.\n";
}
}
?>
The output from the above is:
Testing '{abc}': Got 'abc'.
Testing '\{abc}': REGEX didn't match.
Testing '\{abc\}': REGEX didn't match.
Hope this helps,
Torben
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+