用正则表达式作html2RSS服务
生活随笔
收集整理的這篇文章主要介紹了
用正则表达式作html2RSS服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
h2R的想法已經有了很長時間了,今天終于開始動手。
主要用的是文本匹配:
????????????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卡學生證及紙制學生證招領名單">IC卡學生證及紙制學生證招領名單</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="關于“SRT計劃項目優秀獎”申報的通知">關于“SRT計劃項目優秀獎”申</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立項申請通知">新一輪SRT立項申請通知</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卡學生證及紙制學生證招領名單">IC卡學生證及紙制學生證招領名單</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="關于“SRT計劃項目優秀獎”申報的通知">關于“SRT計劃項目優秀獎”申</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立項申請通知">新一輪SRT立項申請通知</a></td>
正則表達式如是解析:
1、href\\s*=\\s*
匹配href,其后面的=兩側有沒有空格、有幾個空格都可以。
2、(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))
摘取數據1,即link,其兩側有沒有引號都可以。
3、\\s+\\S+\\s+
匹配至少一個空各,緊接著至少一個非空格,緊接著至少一個空各。
其實匹配的是 target=_blank
正在繼續作。
發現正則表達式太強了,簡直就是文本處理的SQL,比SQL還強!
現在覺得,不知自己是為了實現h2R服務而學習Regex,還是為了學習Regex而拿h2R服務做練習。
都挺好。
轉載于:https://www.cnblogs.com/civ3/archive/2005/10/16/256119.html
總結
以上是生活随笔為你收集整理的用正则表达式作html2RSS服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP开发一个简单的成绩录入系统
- 下一篇: linux下dd命令详解