我在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>
以上是我的代码,我想尝试匹配
<tr id="part1AModel.errors"> </tr>
所以,如果它存在我得到它返回但我无法弄清楚它的生命的语法。我用了
/<tr id=\"part1AModel.errors\">(.*?)<\/tr>/
但它不匹配。我在这里缺少什么?
答案 0 :(得分:0)
默认情况下,.
点模式匹配除换行符之外的任何字符。要让.
也与换行符匹配,请使用/s
PCRE_DOTALL
选项。这在dot模式的PHP regexp参考中提到:
http://php.net/manual/en/regexp.reference.dot.php
在字符类之外,模式中的点与主题中的任何一个字符匹配,包括非打印字符,但不是(默认情况下)换行符。如果设置了PCRE_DOTALL选项,则点也匹配换行符。
您可以将此应用于您已验证的以下示例:
/<tr id=\"part1AModel\.errors\">(.*?)<\/tr>/s
我还做了以下更改:
.
期以匹配文字句号,而不是任何字符。?
捕获组之后移动了()
以使捕获可选,如下所示: /<tr id=\"part1AModel\.errors\">([\s\S]*)?<\/tr>/
将此项与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
)
)