|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-02-04 19:30 UTC] mrsnikivan at gmail dot com
Description:
------------
I using worksheet de pear(library parse.php, found problem for preg_match)
$token='=sum($a1:$a1);
if (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$token)) {
...
}
no true
$token='=sum(a1:a1);
if (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$token)) {
...
}
is true
Test script:
---------------
$ change another different character and no problem sample @
$token='=sum(@a1:@a1);
if (preg_match("/^(\@)?[A-Ia-i]?[A-Za-z](\@)?[0-9]+:(\@)?[A-Ia-i]?[A-Za-z](\@)?[0-9]+$/",$token)) {
...
}
After trying other caracters I found that the problem is that the $ replace internal a point (.) character 46
$token='=sum($a1:$a1);
if (preg_match("/^(.)?[A-Ia-i]?[A-Za-z](.)?[0-9]+:(.)?[A-Ia-i]?[A-Za-z](.)?[0-9]+$/",$token)) {
...
}
here if there was affirmative result for dollar ($) and point (.)
Expected result:
----------------
true for (\$) for preg_match
Actual result:
--------------
false for (\$) for preg_match
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 08 01:00:02 2025 UTC |
The pattern you gave is never going to match the token provided, [A-Ia-i] will never match sum; I assume the token has been provided incorrect: <?php $token='$a1:$a1'; var_dump(preg_match("/^(\\$)?[A-Ia-i]?[A-Za-z](\\$)?[0-9]+:(\\$)?[A-Ia-i]?[A-Za- z](\\$)?[0-9]+$/",$token, $matches)); print_r($matches); ?> You need to escape $ twice when using double quotes. <?php $token='$a1:$a1'; var_dump(preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z] (\$)?[0-9]+$/',$token, $matches)); print_r($matches); ?> Both examples function as expected.