分支复位组的反向引用
Disclaimer: I do not need another better regular expression without reset groups. I need to understand why the output is different for PHP 5
and PHP 7
.
免责声明:我不需要另一个更好的正则表达式而不需要重置组。我需要理解为什么PHP 5和PHP 7的输出不同。
Problem: I tried to use a branch reset group to match a string using PHP
which consists of groups of digits separated by different separators.
问题:我尝试使用一个分支复位组来匹配一个使用PHP的字符串,PHP包含由不同分隔符分隔的数字组。
$string = '12-34-56-78';
$pattern = '/^\d{2}(?|(---)|(-)|(\.)|(\:))\d{2}\1\d{2}\1\d{2}$/';
$matches = preg_match($pattern, $string) === 1;
var_dump($matches);
Unfortunately it works only for PHP < 7
. I also checked the version of the libpcre
and it's not the source of the problem. The same version of the libpcre
returns different results for different PHP
versions.
不幸的是,它只适用于PHP < 7。我还检查了libpcre的版本,它不是问题的根源。libpcre的相同版本为不同的PHP版本返回不同的结果。
I wasn't able to find any references to what and why changed in PHP 7
.
在PHP 7中,我找不到任何关于更改内容和原因的引用。
Question: why the output different for PHP 5
and PHP 7
? Is it an expected behavior for PHP 7
?
问:为什么PHP 5和PHP 7的输出不同?这是PHP 7的预期行为吗?
Update: seems to be a bug.
更新:似乎是一个bug。
1 个解决方案
#1
2
Are you sure preg_match
returns 0 and not FALSE ?
您确定preg_match返回0而不是FALSE吗?
Edit
编辑
Still don't know why but inverting (-)
and (---)
solve the problem :
还是不知道为什么反演(-)和(-)解决了这个问题:
/^\d{2}(?|(-)|(---)|(\.)|(\:))\d{2}\1\d{2}\1\d{2}$/
/ ^ \ d { 2 }(? |(-)|(——)|(\)|(\:))\ d { 2 } \ \ d { 2 } \ 1 \ d { 2 } /美元
Demo
演示
Edit 2
编辑2
With PHP 7, it seems only the first subpattern works. The regex will also be falsy with the following code :
在PHP 7中,它似乎只是第一个子模式的工作。regex也将有以下代码:
$string = '12.34.56.78';
$string = '12:34:56:78';
$string = '12---34---56---78';
Maybe a PCRE bug because branch reset synatx seems correct to me.
也许是PCRE错误,因为分支重置synatx在我看来是正确的。
更多相关文章
- 在php中检测浏览器,版本和平台的可靠方法
- centos7 升级php版本
- 在rhel5版本下安装mysql+apache+php实战攻略
- mac下安装多版本PHP及切换
- 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持。
- mysql数据版本控制系统的最佳实践
- 30分钟安装linux版本mysql5.7.21版本,没坑,高效,必会
- MySQL5.7以上版本root用户空密码修改(windows系统、zip版MySQL)
- mysql的zip版本安装填坑