用正则表达式作html2RSS服务
生活随笔
收集整理的這篇文章主要介紹了
用正则表达式作html2RSS服务
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
h2R的想法已經(jīng)有了很長(zhǎng)時(shí)間了,今天終于開(kāi)始動(dòng)手。
主要用的是文本匹配:
????????????XmlNode?channles=root.FirstChild;
????????????Regex?r;
????????????Match?m;
????????????r?=?new?Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))\\s+\\S+\\s+title\\s*=\\s*(?:\"(?<2>[^\"]*)\"|(?<2>\\S+))",RegexOptions.IgnoreCase|RegexOptions.Compiled);
????????????for?(m?=?r.Match(str);?m.Success;?m?=?m.NextMatch())?
????????????{
????????????//????rst+="link="?+?m.Groups[1]?+?"\ntitle="?+?m.Groups[2]+"\n";
????????????????XmlElement?oitem=xml.CreateElement("item");
????????????????XmlElement?o=xml.CreateElement("title");
????????????????o.InnerText=m.Groups[2].Value;
????????????????oitem.AppendChild(o);
????????????????
????????????????o=xml.CreateElement("link");
????????????????o.InnerText=m.Groups[1].Value;
????????????????oitem.AppendChild(o);
????????????????channles.AppendChild(oitem);
????????????}
比如str=?????<tr><td><tr?height=19><td?align=center?width=14><img?src=/icons/info/dot_h.gif?width=5?height=5></td><td?align=left><a?href=/zzh/30630.nsf/(AllDocsByUnid)/C81ECBA70F9A8795C82570990031DE28?opendocument?target=_blank?title="IC卡學(xué)生證及紙制學(xué)生證招領(lǐng)名單">IC卡學(xué)生證及紙制學(xué)生證招領(lǐng)名單</a></td><td?align=right?width=80><font?color=#000066>10-13?18:04</font></td></tr><tr?height=19><td?align=center?width=14><img?src=/icons/info/dot_h.gif?width=5?height=5></td><td?align=left><a?href=/zzh/30630.nsf/(AllDocsByUnid)/81BF13BCCAB992A1C825709900300465?opendocument?target=_blank?title="關(guān)于“SRT計(jì)劃項(xiàng)目?jī)?yōu)秀獎(jiǎng)”申報(bào)的通知">關(guān)于“SRT計(jì)劃項(xiàng)目?jī)?yōu)秀獎(jiǎng)”申</a></td><td?align=right?width=80><font?color=#000066>10-13?17:44</font></td></tr><tr?height=19><td?align=center?width=14><img?src=/icons/info/dot_h.gif?width=5?height=5></td><td?align=left><a?href=/zzh/30630.nsf/(AllDocsByUnid)/713C777073ED05DBC8257099002FE71B?opendocument?target=_blank?title="新一輪SRT立項(xiàng)申請(qǐng)通知">新一輪SRT立項(xiàng)申請(qǐng)通知</a></td>
主要用的是文本匹配:
????????????XmlNode?channles=root.FirstChild;
????????????Regex?r;
????????????Match?m;
????????????r?=?new?Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))\\s+\\S+\\s+title\\s*=\\s*(?:\"(?<2>[^\"]*)\"|(?<2>\\S+))",RegexOptions.IgnoreCase|RegexOptions.Compiled);
????????????for?(m?=?r.Match(str);?m.Success;?m?=?m.NextMatch())?
????????????{
????????????//????rst+="link="?+?m.Groups[1]?+?"\ntitle="?+?m.Groups[2]+"\n";
????????????????XmlElement?oitem=xml.CreateElement("item");
????????????????XmlElement?o=xml.CreateElement("title");
????????????????o.InnerText=m.Groups[2].Value;
????????????????oitem.AppendChild(o);
????????????????
????????????????o=xml.CreateElement("link");
????????????????o.InnerText=m.Groups[1].Value;
????????????????oitem.AppendChild(o);
????????????????channles.AppendChild(oitem);
????????????}
比如str=?????<tr><td><tr?height=19><td?align=center?width=14><img?src=/icons/info/dot_h.gif?width=5?height=5></td><td?align=left><a?href=/zzh/30630.nsf/(AllDocsByUnid)/C81ECBA70F9A8795C82570990031DE28?opendocument?target=_blank?title="IC卡學(xué)生證及紙制學(xué)生證招領(lǐng)名單">IC卡學(xué)生證及紙制學(xué)生證招領(lǐng)名單</a></td><td?align=right?width=80><font?color=#000066>10-13?18:04</font></td></tr><tr?height=19><td?align=center?width=14><img?src=/icons/info/dot_h.gif?width=5?height=5></td><td?align=left><a?href=/zzh/30630.nsf/(AllDocsByUnid)/81BF13BCCAB992A1C825709900300465?opendocument?target=_blank?title="關(guān)于“SRT計(jì)劃項(xiàng)目?jī)?yōu)秀獎(jiǎng)”申報(bào)的通知">關(guān)于“SRT計(jì)劃項(xiàng)目?jī)?yōu)秀獎(jiǎng)”申</a></td><td?align=right?width=80><font?color=#000066>10-13?17:44</font></td></tr><tr?height=19><td?align=center?width=14><img?src=/icons/info/dot_h.gif?width=5?height=5></td><td?align=left><a?href=/zzh/30630.nsf/(AllDocsByUnid)/713C777073ED05DBC8257099002FE71B?opendocument?target=_blank?title="新一輪SRT立項(xiàng)申請(qǐng)通知">新一輪SRT立項(xiàng)申請(qǐng)通知</a></td>
正則表達(dá)式如是解析:
1、href\\s*=\\s*
匹配href,其后面的=兩側(cè)有沒(méi)有空格、有幾個(gè)空格都可以。
2、(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))
摘取數(shù)據(jù)1,即link,其兩側(cè)有沒(méi)有引號(hào)都可以。
3、\\s+\\S+\\s+
匹配至少一個(gè)空各,緊接著至少一個(gè)非空格,緊接著至少一個(gè)空各。
其實(shí)匹配的是 target=_blank
正在繼續(xù)作。
發(fā)現(xiàn)正則表達(dá)式太強(qiáng)了,簡(jiǎn)直就是文本處理的SQL,比SQL還強(qiáng)!
現(xiàn)在覺(jué)得,不知自己是為了實(shí)現(xiàn)h2R服務(wù)而學(xué)習(xí)Regex,還是為了學(xué)習(xí)Regex而拿h2R服務(wù)做練習(xí)。
都挺好。
轉(zhuǎn)載于:https://www.cnblogs.com/civ3/archive/2005/10/16/256119.html
總結(jié)
以上是生活随笔為你收集整理的用正则表达式作html2RSS服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PHP开发一个简单的成绩录入系统
- 下一篇: linux下dd命令详解