PHP正则表达式匹配,匹配所有以数字开头的行,后跟句点

时间:2018-05-15 14:16:22

标签: php regex simple-html-dom

我使用simple_html_dom来解析一些HTML并在名为$pre的数组中包含以下HTML表

现在,我如何使用PHP正则表达式只获得与下面结果匹配的行?

<table>
    <tr>
        <td>
            <pre>1.   APEAL/890/2010     HUSSAIN ISMAIL SATWILKAR        SHRI C.K. PENDSE</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>     [Criminal]                                         MS.ROHINI DANDEKAR ADV.AP</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                        V/S THE STATE OF MAHARASH       PTD AS PER CTS ORD 7/9/17</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                        TRA                             P.P.FOR  P. P</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre></pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>        REMARK : (By Accused against Conviction) Note: (1) Matter is Ready for final</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 hearing. (2) Accd. is in jail. (3) R & P with PB received. (4)</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 Muddemal article are to be called for. (5) Report received from</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 Nashik Central Prison stated therein that "Orig. accd. death dated</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 20/11/2015 (Report kept at flag "A") . ....... Court (DB) for final</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 hearing.</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre></pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre></pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre></pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>2.   APEAL/966/2011     ABDUL MALIK SHAIKH              SHRI S. R. MITHARE</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>     [Criminal]</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                        V/S THE STATE OF MAHARASH</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                        TRA</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre></pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>        REMARK : (By Accused Against Conviction) Note:- (1) Matter is ready for</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 Final Hearing. (2) Original Accused is in Jail. (3) R & P received</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 with PaperBooks. (4) Muddemal Articles are to be called for. (5)</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 Report received from Kolhapur central Prison stated therein that</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 "Orig. Accused expired on 19/04/2015 (Report kept at flag "A")</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>                 - Court D.B. for Final Hearing.</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre></pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre></pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre></pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>3.   APEAL/486/2012     AJAY SITARAM BHARATI            MISS. TANU KHATTRI</pre>
        </td>
    </tr>
    <tr>
        <td>
            <pre>     [Criminal]</pre>
        </td>
    </tr>
</table>

使用正则表达式后的结果:

<pre>1.   APEAL/890/2010     HUSSAIN ISMAIL SATWILKAR        SHRI C.K. PENDSE</pre>
<pre>2.   APEAL/966/2011     ABDUL MALIK SHAIKH              SHRI S. R. MITHARE</pre>
<pre>3.   APEAL/486/2012     AJAY SITARAM BHARATI            MISS. TANU KHATTRI</pre>

使用此代码:preg_match('^\<pre\>\d2*\./gm', $pre[$i])返回: preg_match(): No ending delimiter '^' found

这看起来像正确使用的正则表达式,这来自regex101:

^ asserts position at start of the string
\< matches the character < literally (case sensitive)
pre matches the characters pre literally (case sensitive)
\> matches the character > literally (case sensitive)
\d matches a digit (equal to [0-9])
    2* matches the character 2 literally (case sensitive)
    * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\. matches the character . literally (case sensitive)

Global pattern flags
g modifier: global. All matches (don't return after first match)

2 个答案:

答案 0 :(得分:1)

以下是您的需求:

#<pre>(?<line>\d+\..+)<\/pre>#

显然你知道pre是什么。括号表示一个捕获组,我将其命名为“line”,方法是将?<line>放在括号中。

然后它会查找一个数字\d+\,一个文字点\.,任何.+后跟结束标记。

$regex = '#<pre>(?<line>\d+\..+)<\/pre>#';

preg_match_all($regex, $html, $matches);

foreach($matches['line'] as $line) {
    echo $line ."\n";
}

输出:

1. APEAL/890/2010 HUSSAIN ISMAIL SATWILKAR SHRI C.K. PENDSE 
2. APEAL/966/2011 ABDUL MALIK SHAIKH SHRI S. R. MITHARE

这里有效:https://regex101.com/r/6U8S9C/1

再次在php中运行:https://3v4l.org/QoVsY

答案 1 :(得分:0)

php preg_*函数需要一个分隔符 - 一个未在模式中使用的符号。

此外,您的模式无法正确匹配。原因是$stmt = $mysqli->stmt_init(); $stmt->prepare($sql); 匹配行的开头。并且^标记不会针对几个标签启动。

此正则表达式将匹配同一行中的任何pre标记,该标记以至少一个数字(例如,1,16,256等)和句点开头。

pre

在此示例中,我使用preg_match('#(<pre>\d+\..*</pre>)#', $pre[$1], $matches); vaR_dump($matches); 作为分隔符。

相关问题