为什么这个pregmatch不起作用?

时间:2016-03-14 17:15:23

标签: php regex preg-match preg-match-all

我有这个字符串,我试图找到一个匹配,其中一行有端口号,字段总是整数,或包含该字的链接其中port- 。关于为什么我的正则表达式无效的建议将不胜感激。

我正在使用的php表达式是:

preg_match_all("/<td align=\"left\">\s*(<a href=\"http:\/\/www.proxynova.com\/proxy-server-list\/port-\d*|\d*)?\s*<\/td>/s", $input_lines, $output_array);

我工作的Live Regex网站: http://www.phpliveregex.com/p/eX8

我正在寻找的字符串如下:

<table width="950" class="table" id="tbl_proxy_list">
<thead>
    <tr>
      <th>Proxy IP</th>
      <th>Proxy Port</th>
      <th>Last Check</th>
      <th nowrap="nowrap"><span title="Proxy Speed in bytes per second">Proxy Speed</span></th>
      <th>Uptime</th>
      <th><span title="The location of that particular proxy.">Proxy Country</span></th>
      <th>Anonymity </th>
    </tr>
</thead>
<tbody>
    <tr>
        <td align="left">         
            <span class="row_proxy_ip">220.225.87.129</span>
        </td>

        <td align="left">
            <a href="http://www.proxynova.com/proxy-server-list/port-8080">8080</a>
        </td>
        <td align="left">
            <time class="icon icon-check timeago" datetime="2016-03-14 12:58:53Z"></time>
        </td>         
        <td align="left">
            <div class="progress-bar" data-value="7.7100404" title="5855.0202"></div>
        </td>
        <td style="text-align:center !important;">
            <span style="color:#009900;">59%</span>
        </td>
        <td align="left">
            <img src="//www.proxynova.com/assets/images/blank.gif" class="flag flag-in" width="15" height="11" alt="IN" />
            <a href="/proxy-server-list/country-in/">India        
                <span class="proxy-city"> - Chandannagar </span> 
            </a>
        </td>
        <td align="left">
            <span class="proxy_transparent" style="font-weight:bold; font-size:10px;">Transparent</span>
        </td>
    </tr>
    <tr>
        <td align="left">         
            <span class="row_proxy_ip">220.225.87.129</span>
        </td>

        <td align="left">
            <a href="http://www.proxynova.com/proxy-server-list/port-8080">8080</a>
        </td>
        <td align="left">
            <time class="icon icon-check timeago" datetime="2016-03-14 12:58:53Z"></time>
        </td>         
        <td align="left">
            <div class="progress-bar" data-value="7.7100404" title="5855.0202"></div>
        </td>
        <td style="text-align:center !important;">
            <span style="color:#009900;">59%</span>
        </td>
        <td align="left">
            <img src="//www.proxynova.com/assets/images/blank.gif" class="flag flag-in" width="15" height="11" alt="IN" />
            <a href="/proxy-server-list/country-in/">India        
                <span class="proxy-city"> - Chandannagar </span> 
            </a>
        </td>
        <td align="left">
            <span class="proxy_transparent" style="font-weight:bold; font-size:10px;">Transparent</span>
        </td>
    </tr>
    <tr>
        <td align="left">         
            <span class="row_proxy_ip">220.225.87.129</span>
        </td>

        <td align="left">
            80
        </td>
        <td align="left">
            <time class="icon icon-check timeago" datetime="2016-03-14 12:58:53Z"></time>
        </td>         
        <td align="left">
            <div class="progress-bar" data-value="7.7100404" title="5855.0202"></div>
        </td>
        <td style="text-align:center !important;">
            <span style="color:#009900;">59%</span>
        </td>
        <td align="left">
            <img src="//www.proxynova.com/assets/images/blank.gif" class="flag flag-in" width="15" height="11" alt="IN" />
            <a href="/proxy-server-list/country-in/">India        
                <span class="proxy-city"> - Chandannagar </span> 
            </a>
        </td>
        <td align="left">
            <span class="proxy_transparent" style="font-weight:bold; font-size:10px;">Transparent</span>
        </td>
    </tr>
</tbody>
</table>

1 个答案:

答案 0 :(得分:2)

试试这个 -

<td align=\"left\">\s*(<a href=\"http:\/\/www\.proxynova\.com\/proxy-server-list\/port-\d+|\d+).*?\s*<\/td>


Example here

所以你错过了几件事 -
*末尾的\分隔符
*一旦满足您的选择,忽略捕获组后的其余字符。

代码示例 -

preg_match_all(
    "/<td align=\"left\">\s*(<a href=\"http:\/\/www.proxynova.com\/proxy-server-list\/port-\d+|\d+).*?\s*<\/td>/",
    $str,
    $output_array
);