doukan5332 2013-05-06 13:22
浏览 15
已采纳

[^ pL]奇怪的正则表达式行为

I have a snippet of PHP that replaces all characters from the left not being Unicode letters. It works fine, with one exception, and I can't figure out why. Can anyone help?

<?php
$B=$A;
do{
    $A=$B;
    $B=preg_replace('/^[^\pL\s]/','',$B);
    }
while($B!=$A);
echo $B;
?>

If I feed it with a string like "\\*^&\\\@@\816.80831téstmé" it nicely spits out "téstmé".
$A="*^&\\\@@\816.80831[+" gives an empty string, also correct.
But, when I enter "\\*^&\\\@@\816.80831", I end up with "831", when in fact it should be an empty string.

"^&\\\@@\8016.8048.31" gives "48.31"
"^&\\\@@\8016.8148.31" gives an empty string correctly
"^&\\\@@\8016.8148067" gives "16.8148067"
"^&\\\@@\8116.8148167" the again is empty
It seems to have somethinh to do with the zero and the dot, but I can't find a pattern nor a solution. I tried adding strval, but still the same result. Maybe someone has an answer? Thnx.

  • 写回答

1条回答 默认 最新

  • duanjuelu8874 2013-05-06 13:36
    关注

    I honestly can not find out why this is going wrong. It has to be some sort of bug. However there is a simple solution.

    <?php
    $B=preg_replace('/^[^\pL\s]*/','',$A);
    

    This way it has the same functionality, except it works and has a lot less overhead.

    Update: i did some testing in Java, regex coach and regexpal.com and they all do it correctly. So this has to be a bug in preg_replace.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集