这个正则表达式怎么写? 在下面的页面中匹配出列表中的“商品名称”和“链接地址”,以及对应“价格链接”http://www.newegg.com.cn/Search.aspx?keyword=ipad2 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #! /bin/bash#shell codewget http://www.newegg.com.cn/Search.aspx?keyword=ipad2 response.txt$s = file_get_contents('response.txt');$p = '{<div class="itemCell">.*?<div class="title">.*?<a href="([^"]+?)".*?>([^<]+?)</a>.*?</div>.*?</div>}s';preg_match_all($p, $s, $m);print_r($m); #结果中的第1,2项为 [1] => Array ( [0] => http://www.newegg.com.cn/Product/A36-184-7G8.htm [1] => http://www.newegg.com.cn/Product/A36-184-7GC.htm [2] => http://www.newegg.com.cn/Product/A60-0XF-037.htm [3] => http://www.newegg.com.cn/Product/A60-0XF-035.htm [4] => http://www.newegg.com.cn/Product/A60-0XF-05H-03.htm [5] => http://www.newegg.com.cn/Product/A60-0XF-05G-05.htm [6] => http://www.newegg.com.cn/Product/A60-0XF-05H-04.htm [7] => http://www.newegg.com.cn/Product/A60-0XF-05G-04.htm [8] => http://www.newegg.com.cn/Product/A60-0XF-034.htm [9] => http://www.newegg.com.cn/Product/A60-0XF-05H-02.htm [10] => http://www.newegg.com.cn/Product/A60-0XF-038.htm [11] => http://www.newegg.com.cn/Product/A60-0XF-05H-05.htm [12] => http://www.newegg.com.cn/Product/A60-0XF-05H.htm [13] => http://www.newegg.com.cn/Product/A60-0XF-05J.htm [14] => http://www.newegg.com.cn/Product/A60-0XF-05F.htm [15] => http://www.newegg.com.cn/Product/A60-0XF-036.htm [16] => http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm [17] => http://www.newegg.com.cn/Product/A60-0XF-05J-02.htm [18] => http://www.newegg.com.cn/Product/A60-0XF-033.htm [19] => http://www.newegg.com.cn/Product/A60-0XF-05G-03.htm [20] => http://www.newegg.com.cn/Product/A60-0XF-05J-03.htm [21] => http://www.newegg.com.cn/Product/A60-0XF-032.htm [22] => http://www.newegg.com.cn/Product/A60-0LA-04T.htm [23] => http://www.newegg.com.cn/Product/A60-184-047.htm ) [2] => Array ( [0] => Apple 苹果 iPad2 平板电脑 MC916CH/A 64G WIFI版 黑色 [1] => Apple 苹果 iPad2 平板电脑 MC981CH/A 64G WIFI版 白色 [2] => TUNEWEAR 腾威尔 IPAD2-TUN-FILM-01保护膜 高透 [3] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-02 柔软硅胶保护套 粉红 (含膜) [4] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR03 尼龙单肩挎包 红色(含膜) [5] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-05 PU皮包 驼色配棕 (含膜) [6] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR04 尼龙单肩挎包 蓝色(含膜) [7] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-04 PU皮包 棕配黑 (含膜) [8] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-01 柔软硅胶保护套 浅灰 (含膜) [9] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR02 尼龙单肩挎包 棕色(含膜) [10] => TUNEWEAR 腾威尔 IPAD2-TUN-FILM-02 屏幕保护膜 防眩目 磨砂 [11] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR05 尼龙单肩挎包 肉色(含膜) [12] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR01 尼龙单肩挎包 黑色(含膜) [13] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC03 超薄硬保护胶套 (带smartcover接口)白色 [14] => Tunewear 腾威尔 IPAD2-CARBON-01 仿碳纤硬保护套 黑色 (含膜) [15] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-03 柔软硅胶保护套 蓝色 (含膜) [16] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜) [17] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC02 超薄硬保护胶套 (带smartcover接口)浅灰 [18] => TUNEWEAR 腾威尔 IPAD2-EGG-SHELL-01 蛋壳质感超薄保护壳 透明 (含膜) [19] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-03 PU皮包 橙配黑 (含膜) [20] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC01 超薄硬保护胶套 (带smartcover接口)透明 [21] => TUNEWEAR 腾威尔 IPAD2-EGG-SHELL-02 蛋壳质感超薄保护壳 浅灰 (含膜) [22] => i-kodoo 爱酷多 ipad2 smart cover 智能四折立式 PU套 CPI-20 灰色 [23] => Apple 苹果 iPad2 原装保护套 Smart Cover MC945FE/A 聚氨酯 橙色 ) 这个可以用DOM解析,比如说getElementsByTagname、现在js里也支持getelementByClass了,这样解析那个HTML就更简单了,具体你再查查资料、、这只是思路。当然你也可以用正则,<div class="itemCell"><dl class="inner"><dt><p class="brand"></p><p class="iconPromB"><span><img src="http://c2.neweggimages.com.cn/WebResources/2009/Default/Nest/img/promIcon/promIcon04.gif"/></span></p> <a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm"><img src="http://c1.neweggimages.com.cn/neweggpic2/P144/A60/0XF/A60-0XF-05G-02.jpg" title="Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)" /></a></dt><dd class="pdInfo"><div class="title"><p class="info"><a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm" title="Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)">Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)</a></p> <p class="prom null"></p> <p class="extraIcon null"></p></div><ul class="bullet"><LI>TUNEWEAR 腾威尔<LI>笔记簿形保护套<LI>IPAD2-TUN-FOLIO-05</ul></dd><dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p> </dd><dd class='rank '></dd><dd class="btnArea"><a href="http://www.newegg.com.cn/Shopping/ShoppingCart.aspx?action=Add&productno=165766" class="cmnbtn btnG"><span>购买</span></a><a href="https://secure.newegg.com.cn/Customer/WishList.aspx?action=Add&productno=165766" class="cmnbtn btnH"><span>收藏</span></a></dd></dl></div> 如上面的代码:这是链接地址:<p class="info"><a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm" title="Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)">Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)</a></p>这是商品名称和价格:<ul class="bullet"><LI>TUNEWEAR 腾威尔<LI>笔记簿形保护套<LI>IPAD2-TUN-FOLIO-05</ul></dd><dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p>以价格为例:<dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p>可以这样写,只需做些代换:<dd class="price"><p class="bypast"><span>¥<!--这里换成浮点数的正则,如 \s ^(-?\d+)(\.\d+)? \s --></span></p><p class="current"><strong class="price"><span>¥</span> \s ^(-?\d+)(\.\d+)? \s</strong></p>如果上面的不行,将里面的< /> 等特殊符号,转义一下,或用实体表示试下,如果不对,请指教 关于urlencode? php有调用脚本后立即返回的函数吗? php 采用什么ajax 框架好! 为何无法调用外部程序?郁闷 $a = "123 456";echo $a;次字符串含有4个空格,可是echo到页面就只剩下1个空格,这是为什么呢? 求找bug,谢谢了 php的mail函数给某些email提供商的邮箱发邮件,接收不到 php在写入ACESS数据库时怎么写入不了长度超过50的内容啊? 这个论坛好像不是很好用? php难题!请帮帮我! 文本框中的两个日期想减,得到月份,和天数. JS如何把内容放入到剪切板?
#! /bin/bash
#shell code
wget http://www.newegg.com.cn/Search.aspx?keyword=ipad2 response.txt$s = file_get_contents('response.txt');
$p = '{<div class="itemCell">.*?<div class="title">.*?<a href="([^"]+?)".*?>([^<]+?)</a>.*?</div>.*?</div>}s';
preg_match_all($p, $s, $m);
print_r($m);
#结果中的第1,2项为
[1] => Array
(
[0] => http://www.newegg.com.cn/Product/A36-184-7G8.htm
[1] => http://www.newegg.com.cn/Product/A36-184-7GC.htm
[2] => http://www.newegg.com.cn/Product/A60-0XF-037.htm
[3] => http://www.newegg.com.cn/Product/A60-0XF-035.htm
[4] => http://www.newegg.com.cn/Product/A60-0XF-05H-03.htm
[5] => http://www.newegg.com.cn/Product/A60-0XF-05G-05.htm
[6] => http://www.newegg.com.cn/Product/A60-0XF-05H-04.htm
[7] => http://www.newegg.com.cn/Product/A60-0XF-05G-04.htm
[8] => http://www.newegg.com.cn/Product/A60-0XF-034.htm
[9] => http://www.newegg.com.cn/Product/A60-0XF-05H-02.htm
[10] => http://www.newegg.com.cn/Product/A60-0XF-038.htm
[11] => http://www.newegg.com.cn/Product/A60-0XF-05H-05.htm
[12] => http://www.newegg.com.cn/Product/A60-0XF-05H.htm
[13] => http://www.newegg.com.cn/Product/A60-0XF-05J.htm
[14] => http://www.newegg.com.cn/Product/A60-0XF-05F.htm
[15] => http://www.newegg.com.cn/Product/A60-0XF-036.htm
[16] => http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm
[17] => http://www.newegg.com.cn/Product/A60-0XF-05J-02.htm
[18] => http://www.newegg.com.cn/Product/A60-0XF-033.htm
[19] => http://www.newegg.com.cn/Product/A60-0XF-05G-03.htm
[20] => http://www.newegg.com.cn/Product/A60-0XF-05J-03.htm
[21] => http://www.newegg.com.cn/Product/A60-0XF-032.htm
[22] => http://www.newegg.com.cn/Product/A60-0LA-04T.htm
[23] => http://www.newegg.com.cn/Product/A60-184-047.htm
) [2] => Array
(
[0] => Apple 苹果 iPad2 平板电脑 MC916CH/A 64G WIFI版 黑色
[1] => Apple 苹果 iPad2 平板电脑 MC981CH/A 64G WIFI版 白色
[2] => TUNEWEAR 腾威尔 IPAD2-TUN-FILM-01保护膜 高透
[3] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-02 柔软硅胶保护套 粉红 (含膜)
[4] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR03 尼龙单肩挎包 红色(含膜)
[5] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-05 PU皮包 驼色配棕 (含膜)
[6] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR04 尼龙单肩挎包 蓝色(含膜)
[7] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-04 PU皮包 棕配黑 (含膜)
[8] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-01 柔软硅胶保护套 浅灰 (含膜)
[9] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR02 尼龙单肩挎包 棕色(含膜)
[10] => TUNEWEAR 腾威尔 IPAD2-TUN-FILM-02 屏幕保护膜 防眩目 磨砂
[11] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR05 尼龙单肩挎包 肉色(含膜)
[12] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR01 尼龙单肩挎包 黑色(含膜)
[13] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC03 超薄硬保护胶套 (带smartcover接口)白色
[14] => Tunewear 腾威尔 IPAD2-CARBON-01 仿碳纤硬保护套 黑色 (含膜)
[15] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-03 柔软硅胶保护套 蓝色 (含膜)
[16] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)
[17] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC02 超薄硬保护胶套 (带smartcover接口)浅灰
[18] => TUNEWEAR 腾威尔 IPAD2-EGG-SHELL-01 蛋壳质感超薄保护壳 透明 (含膜)
[19] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-03 PU皮包 橙配黑 (含膜)
[20] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC01 超薄硬保护胶套 (带smartcover接口)透明
[21] => TUNEWEAR 腾威尔 IPAD2-EGG-SHELL-02 蛋壳质感超薄保护壳 浅灰 (含膜)
[22] => i-kodoo 爱酷多 ipad2 smart cover 智能四折立式 PU套 CPI-20 灰色
[23] => Apple 苹果 iPad2 原装保护套 Smart Cover MC945FE/A 聚氨酯 橙色
)
<p class="brand"></p><p class="iconPromB"><span>
<img src="http://c2.neweggimages.com.cn/WebResources/2009/Default/Nest/img/promIcon/promIcon04.gif"/>
</span></p>
<a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm"><img src="http://c1.neweggimages.com.cn/neweggpic2/P144/A60/0XF/A60-0XF-05G-02.jpg" title="Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)" /></a></dt>
<dd class="pdInfo"><div class="title">
<p class="info"><a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm" title="Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)">Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)</a></p>
<p class="prom null"></p>
<p class="extraIcon null"></p>
</div>
<ul class="bullet"><LI>TUNEWEAR 腾威尔<LI>笔记簿形保护套<LI>IPAD2-TUN-FOLIO-05</ul></dd>
<dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p>
</dd>
<dd class='rank '></dd>
<dd class="btnArea"><a href="http://www.newegg.com.cn/Shopping/ShoppingCart.aspx?action=Add&productno=165766" class="cmnbtn btnG"><span>购买</span></a><a href="https://secure.newegg.com.cn/Customer/WishList.aspx?action=Add&productno=165766" class="cmnbtn btnH"><span>收藏</span></a></dd></dl>
</div>
如上面的代码:这是链接地址:<p class="info"><a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm" title="Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)">Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 (含膜)</a></p>这是商品名称和价格:
<ul class="bullet"><LI>TUNEWEAR 腾威尔<LI>笔记簿形保护套<LI>IPAD2-TUN-FOLIO-05</ul></dd>
<dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p>以价格为例:
<dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p>
可以这样写,只需做些代换:
<dd class="price"><p class="bypast"><span>¥<!--这里换成浮点数的正则,如 \s ^(-?\d+)(\.\d+)? \s --></span></p><p class="current"><strong class="price"><span>¥</span> \s ^(-?\d+)(\.\d+)? \s</strong></p>如果上面的不行,将里面的< /> 等特殊符号,转义一下,或用实体表示试下,如果不对,请指教