dou426098 2016-11-20 19:20
浏览 127
已采纳

PHP preg_match完全匹配并在括号内得到什么

Lets say I have the following string:

"**link(http://google.com)*{Google}**"

And I want to use preg_match to find the EXACT text **link(http://google.com) but the text inside the brackets changes all the time. I used to use:

preg_match('#\((.*?)\)#', $text3, $match2);

Which would get what is inside the brackets which is good but if I had: *hwh(http://google.com)** it would get whats inside of that. So how can i get whats inside the brackets if, in front of the brackets has **link?

  • 写回答

3条回答 默认 最新

  • dongliao4353 2016-11-20 19:24
    关注

    ~(?:\*\*link\(([^\)]+)\))~ will match contents in the brackets for all inputs that look like **link(URL) but do not contain extra ) inside URLs. See the example on Regexr: http://regexr.com/3en33 . The whole example:

    $text = '"**link(http://google.com)*{Google}**"
    **link(arduino.cc)*{official Arduino site}';
    $regex = '~(?:\*\*link\((?<url>[^)]+))~';
    preg_match_all($regex, $text, $matches);
    
    var_dump($regex, $matches['url']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献