如何在PHP中使用正则表达式进行匹配

如何在PHP中使用正则表达式进行匹配,php,regex,Php,Regex,我在一个HTML页面上有一些文本,我想用正则表达式来匹配,但我有一个困难的时间 <tr id="part1AModel.errors"> <td colspan="5"> <h3><font color="red">Validation Error</font></h3>You must correct the following error(s) before

我在一个HTML页面上有一些文本,我想用正则表达式来匹配,但我有一个困难的时间

<tr id="part1AModel.errors">
             <td colspan="5">  
                <h3><font color="red">Validation Error</font></h3>You must correct the following error(s) before proceeding:
                <ul>

                        <li><font color="red">The requested effective date entered is not an expedited date. If an expedite is required, please either leave the default effective date or change the effective date to a date that is less than 31 days. If the effective date entered is the date you want, please be sure to select "No"Â for Requested Expedite Treatment and uncheck the Earliest Effective Date checkbox.<br/></font> </li>

                </ul>  
                For assistance, please contact the Help Desk.<hr> 
             </td></tr> 

验证错误在继续之前,必须更正以下错误:
  • 输入的请求生效日期不是加急日期。如果需要加快,请保留默认生效日期或将生效日期更改为少于31天的日期。如果输入的生效日期是您想要的日期,请确保为请求的加速治疗选择“否”,并取消选中“最早生效日期”复选框。
如需帮助,请联系服务台。
以上是我的代码,我想尝试与

<tr id="part1AModel.errors"> </tr>

因此,如果它存在,我将返回它,但我无法找出它的整个生命周期的语法。我曾经

/<tr id=\"part1AModel.errors\">(.*?)<\/tr>/
/(*)/

但它并不匹配。这里缺少什么?

默认情况下,
点模式匹配除换行符以外的任何字符。要使
也与换行符匹配,请使用
/s
PCRE\u DOTALL
选项。这在点模式的PHP regexp参考中提到:

在字符类之外,图案中的点与主题中的任何一个字符匹配,包括非打印字符,但(默认情况下)不匹配换行符。如果设置了PCRE_DOTALL选项,则点也会匹配换行符

您可以将其应用于您的示例,如下所示,我已经验证过:

/(.*)/s

我还做了以下修改:

  • 转义
    句点以匹配文字句点而不是任何字符
  • 移动到
    ()
    捕获组之后,使捕获成为可选的,如下所示:
/([\s\s]*)?/

将此与
preg_match
一起使用将返回:

Array
(
[0] => Array
    (
        [0] => <tr id="part1AModel.errors">
         <td colspan="5">  
            <h3><font color="red">Validation Error</font></h3>You must correct the following error(s) before proceeding:
            <ul>

                    <li><font color="red">The requested effective date entered is not an expedited date. If an expedite is required, please either leave the default effective date or change the effective date to a date that is less than 31 days. If the effective date entered is the date you want, please be sure to select "No"Â for Requested Expedite Treatment and uncheck the Earliest Effective Date checkbox.<br/></font> </li>

            </ul>  
            For assistance, please contact the Help Desk.<hr> 
         </td></tr>
        [1] => 0
    )

[1] => Array
    (
        [0] => 
         <td colspan="5">  
            <h3><font color="red">Validation Error</font></h3>You must correct the following error(s) before proceeding:
            <ul>

                    <li><font color="red">The requested effective date entered is not an expedited date. If an expedite is required, please either leave the default effective date or change the effective date to a date that is less than 31 days. If the effective date entered is the date you want, please be sure to select "No"Â for Requested Expedite Treatment and uncheck the Earliest Effective Date checkbox.<br/></font> </li>

            </ul>  
            For assistance, please contact the Help Desk.<hr> 
         </td>
        [1] => 28
    )
)
数组
(
[0]=>阵列
(
[0] => 
验证错误在继续之前,必须更正以下错误:
  • 输入的请求生效日期不是加急日期。如果需要加急,请保留默认生效日期或将生效日期更改为小于31天的日期。如果输入的生效日期是您想要的日期,请确保选择“否”对于请求的加速治疗,取消选中“最早生效日期”复选框。
如需帮助,请联系服务台。
[1] => 0 ) [1] =>阵列 ( [0] => 验证错误在继续之前,必须更正以下错误:
  • 输入的请求生效日期不是加急日期。如果需要加急,请保留默认生效日期或将生效日期更改为小于31天的日期。如果输入的生效日期是您想要的日期,请确保选择“否”对于请求的加速治疗,取消选中“最早生效日期”复选框。
如需帮助,请联系服务台。
[1] => 28 ) )
我认为,如果您想匹配跨换行符,您需要做一些事情。。您也应该在ID=部分中省略点。考虑使用XML解析扩展(如DOM或XMLRead)。<代码> S(pCREY-DOOLAST)< /代码>。如果设置了该修饰符,模式中的点元字符与所有字符(包括换行符)匹配。没有它,新行被排除在外。此修饰符相当于Perl的/s修饰符。像[^A]这样的负数类始终与换行符匹配,与此修饰符的设置无关。谢谢@linden2015。关于PHP和Perl的好信息。我更新了答案以反映这一点,并引用了PHP regexp点模式。