duandao7704 2018-05-15 14:16
浏览 303
已采纳

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

I used simple_html_dom, to parse some HTML and have the following HTML Table that in an array called $pre

Now, how can I use PHP regex to get only the lines that match the result below?

<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>

Result after using Regex:

<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>

Using this code: preg_match('^\<pre\>\d2*\./gm', $pre[$i]) returns: preg_match(): No ending delimiter '^' found

This looks like the correct regex to use, this is from 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条回答 默认 最新

  • dongmaijie5200 2018-05-15 14:23
    关注

    The php preg_* functions require a delimiter - a symbol that is not used within the pattern.

    Also, your pattern won't match correctly. The reason for this is that ^ matches the very start of the line. And the pre tag doesn't start for a few tabs.

    This regex will match any pre tag that is on the same line, that starts with at least one number (for example, 1, 16, 256, etc) and a period.

    preg_match('#(<pre>\d+\..*</pre>)#', $pre[$1], $matches);
    vaR_dump($matches);
    

    In this example, I've used # as the delimiter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘