RE: Bug in look-behind assertions in PCRE patterns ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ian Thurlbeck wrote:
> Dear All
>
> Is this a bug ?
[...]
> $line = '$res = $bar("ddd", "dfdf");';
> if (preg_match("/(?<!\$)(bar)/", $line, $matches)) {
>          echo "Should NOT match \$bar, but found: ".$matches[1];
> }
> ----------------
>
> In the first preg_match() is correctly ignores the foobar
> function name. However the second preg_match() does NOT
> ignore the $bar as I expected.

This is a quoting issue.  Above you are using double quotes, which interpolates
embedded PHP variables.  Therefore if you want a literal "$" inside a double-quoted
string you have to escape it, as you have done.  However, the "$" character is ALSO
a preg metacharacter (matches end of line).  This means that you must also escape it
for the preg parser as well, so you need to escape it twice, by putting another
literal backslash behind it:

if (preg_match("/(?<!\\\$)(bar)/", $line, $matches)) {

Either that or you can use single quotes instead of double quotes:

if (preg_match('/(?<!\$)(bar)/', $line, $matches)) {

Either one will work.

HTH

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux