dongle0396 2019-07-09 10:52
浏览 54
已采纳

问题正则表达模式寻找类似匹配

I have one of the four patter:

"Test"
'Test'
`Test`
(Test)

Is it possible to get "Test" with a single preg_match call?

I tried the following:

if ( preg_match( '/^(?:"(.*)"|\'(.*)\'|`(.*)`|\((.*)\')$/iu', $pattern, $matches ) )

... but this gives me five elements of $matches back. But I would like to have two only (One for the whole match and one for the found match with "Test" in it.)

  • 写回答

4条回答 默认 最新

  • douying9296 2019-07-09 11:01
    关注

    You can use dot to match the characters aroun d the word and use array_unique to remove duplicates.

    preg_match_all("/.(\w+)./", $str,$match);
    foreach($match as &$m) $m = array_unique($m);
    
    var_dump($match);
    

    https://3v4l.org/T2hnh

    array(2) {
      [0]=>
      array(4) {
        [0]=>
        string(6) ""Test""
        [1]=>
        string(6) "'Test'"
        [2]=>
        string(6) "`Test`"
        [3]=>
        string(6) "(Test)"
      }
      [1]=>
      &array(1) {
        [0]=>
        string(4) "Test"
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序