【问题标题】:preg_match_all with special characterspreg_match_all 带有特殊字符
【发布时间】:2023-03-14 00:25:01
【问题描述】:

我正在尝试对包含我要提取的多个短语的数据执行 preg_match_all。

数据:

'us/Llane/Hówl' then some other text then 'us/Casey/Hówl' and so on

我想将 Llane 和 Casey 这两个名字提取到一个数组中。我目前正在使用http://www.phpliveregex.com/ 以及我的代码本身来尝试解决这个问题,但即使有一些互联网上的优秀指南,正则表达式似乎也很难理解。据我所知,这应该可行:

preg_match_all("/us\/(.*?)\/HÓWL'/",$data,$output);

但我得到的只是 $output[0] 和 $output[1] 两者都是空白的。我之前没有遇到问题,所以它可能是特殊字符,但是我只能找到有关 preg_match_all 的信息以检测特殊字符,而不仅仅是在字符串中使用它们。任何帮助都会很棒,我已经在这个问题上卡住了大约 4 天,并且花了很多时间来解决这个问题。

【问题讨论】:

    标签: php regex special-characters preg-match-all


    【解决方案1】:

    您正在尝试匹配 HÓWL 而不是 Hówl..

    $data = "'us/Llane/Hówl' then some other text then 'us/Casey/Hówl' and so on";
    preg_match_all("~us/(.*?)/Hówl~", $data, $output);
    print_r($output[1]);
    

    输出

    Array
    (
        [0] => Llane
        [1] => Casey
    )
    

    或者,除非您知道Hówl 将始终位于正斜杠的右侧,否则我会考虑使用完整的字母 Unicode property \p{L}。这也将允许您匹配重音字符。

    preg_match_all("~us/(.*?)/\p{L}+~u", $data, $output);
    

    【讨论】:

      【解决方案2】:

      大小写不敏感可能无法正常工作

      使用这个:

      $regex = '~us/\K.*?(?=/Hówl)~';
      $count = preg_match_all($regex, $yourstring, $matches);
      if($count) print_r($matches[0]);
      

      比赛:

      Llane
      Casey
      

      查看the demo中的匹配项。

      说明

      • us/ 匹配文字字符
      • \K 告诉引擎从它返回的最终匹配中删除到目前为止匹配的内容
      • .*? 懒惰匹配到...
      • 前瞻(?=/Hówl) 可以断言后面是Hówl 的点

      【讨论】:

      • 仅供参考,添加了演示和解释,如果您有任何问题,请告诉我。 :)
      猜你喜欢
      • 2012-10-06
      • 2018-11-14
      • 2012-09-15
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多