【问题标题】:PHP Preg_match_all regexp patternPHP Preg_match_all 正则表达式模式
【发布时间】:2012-09-03 06:59:25
【问题描述】:

我需要你的帮助来完成我自己无法完成的模式。

我得到了这个文件:

    description1="SIOUH : Authentification Cerbere - Acces"
    description2="Acces formulaire authentification SIOUH"
    addheader="Pragma: no-cache "
    method="get"
    url="${AUTH_URL}/login/LoginDispatchAction.do?appId=${APP_ID}&backURL=${APP_URL_ENC}"
    errormessage="Phase 1 : Impossible atteindre le formulaire authentification CERBERE pour SIOUH"
    serveur="yes"
    verifypositive="loginConnectAppliForm"
    logrequest="yes" logresponse="no" />

我有这个完美匹配的 preg_match_all :

preg_match_all  ( '#(.*?)="(.*?)"#s', $contents, $matches, PREG_SET_ORDER ); //Chaque option

我的问题是我可以有简单的引号而不是双引号,我该如何改变我的模式来处理它? 例如,我需要像所有其他人一样抓住这条线:

parseresponse='name="conversationId" value="|"'

非常感谢您的帮助。

【问题讨论】:

    标签: php regex preg-match-all


    【解决方案1】:

    使用反向引用要求结束引号与开始引号匹配:

    '#(.*?)=([\'"])(.*?)\2#'
    

    【讨论】:

    • 完美!不知道有可能做到这一点。我在 '#(.*?)=(?:(?:\'(.*?)\')|(?:"(.*?)"))#' 但你的更好。谢谢!
    【解决方案2】:

    尝试使用正则表达式,这样

    ^(.*?)=[\'"](.*?)[\'"]$
    

    【讨论】:

    • 因为我可以在 1 行上有 [logrequest = "yes" logresponse = "yes"],所以我不能使用 ^ 和 $ i nmy 正则表达式。如果我使用 [\'"] 而不是我的 ",我得到这个: Array ([0] => parseresponse='name=" [1] => parseresponse [2] => name= ) 因为我有一个双引号在我的简单引用(parsereponse 是一种模式)中被捕获并且不应该被捕获。
    • 然后尝试使用贪婪捕获 - 删除?从第二组圆括号
    • 使用 '#(\w*?)=["\'](.*)["\']#' 我遇到了 [logrequest = "yes" logresponse = "yes "] 在同一行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 2014-02-24
    • 2016-08-08
    相关资源
    最近更新 更多