doubi1928 2013-02-08 09:32
浏览 20
已采纳

为什么我的RegEx只有在包含一些额外字符时才会结束?

Okay, let's say I'm trying to parse this:

href="http://google.com">Website</a>

This doesn't work:

preg_match('/href="http:\/\/(.+)">/', $a, $b);

But this does:

preg_match('/href="http:\/\/(.+)">Website/', $a, $b);

Why is this? The first one just keeps going to the end of the string.

  • 写回答

2条回答 默认 最新

  • douqi5079 2013-02-08 09:37
    关注

    Greedy selector

    The problem here is that your selector is greedy - it will match the longest possible string it can:

    preg_match('/href="http:\/\/(.+)">/', $a, $b);
    

    Since you use "any character" (.) it is matching right up until the very last occurrence of "> that exists in the input string.

    None-greedy selector

    If you make the selector none-greedy, then it will stop at the shortest possible match:

    preg_match('/href="http:\/\/(.+?)">/', $a, $b);
    

    Note the extra ? in the regex pattern, which modifies the "one or more" (+) operator to be none greedy.

    You can also make all selectors none-greedy by default by using the u modifier:

    preg_match('/href="http:\/\/(.+)">/u', $a, $b);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 使用 java+selenium 接管手动打开的IE浏览器
  • ¥15 ios 新安装app收不到fcm推送
  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?