天天看點

正規表達式的初體驗:分析字元串資料

在程式中,對于資料的分析是非常重要及常見的。對于字元串資料的分析,往往就是對字元串的内容提取,捕獲和替換。除非你要做的分析是簡單的要求,否則你就不要再使用string的IndexOf、LastIndexOf、StartsWith 和 EndsWith 方法了,而是應該改用正規表達式搜尋字元串。

正規表達式雖然學習起來非常的另人頭痛和難過,但他也實在是非常的有效。

.NET Framework 正規表達式引擎是回溯的正規表達式比對器,它并入了傳統的非确定性有限自動機 (NFA) 引擎,雖然相比純正規表達式确定性有限自動機 (DFA) 引擎來講,速度上面有點慢,但功能更加強大了。

通過強大的正規表達式,可以作很多分析服務

1

正規表達式的初體驗:分析字元串資料

 //要求整個數字可以有-号,精度必須為2

2

正規表達式的初體驗:分析字元串資料

 string pattern = @"^-?/d+(/./d{2})?$";

3

正規表達式的初體驗:分析字元串資料

 System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(pattern);

4

正規表達式的初體驗:分析字元串資料

 string[] patternTest ={ "-12.23", "+12.23", "22.3", "22", "=2.34", "2.34.45", "3242 ", "343.343", "" };

5

正規表達式的初體驗:分析字元串資料

 for (int i = 0; i <= patternTest.Length - 1; i++)

6

正規表達式的初體驗:分析字元串資料

 {

7

正規表達式的初體驗:分析字元串資料

 System.Console.WriteLine("{0} is {1}", patternTest[i], rx.IsMatch(patternTest[i]));

8

正規表達式的初體驗:分析字元串資料

 }

運作結果為

-12.23 is True

+12.23 is False

22.3 is False

22 is True

=2.34 is False

2.34.45 is False

3242 is False

343.343 is False

is False

修正了正規表達式,又可以對資料有其他的分析

正規表達式的初體驗:分析字元串資料

 string pattern = @"^[-|+]?/d+(/./d{0,2})?$";

正規表達式的初體驗:分析字元串資料
正規表達式的初體驗:分析字元串資料

 string[] patternTest ={ "-12.23", "+12.23", "22.3", "22", "=2.34", "2.34.45", "32. ","343.343", "" };

正規表達式的初體驗:分析字元串資料
正規表達式的初體驗:分析字元串資料
正規表達式的初體驗:分析字元串資料
正規表達式的初體驗:分析字元串資料

運作結果是

+12.23 is True

22.3 is True

32. is False

依據這個例子,可以作一個很有效的分析使用者輸入的程式了

以下是一個向遠端請求http頁面的,根據傳回的結果,查找url連接配接的demo,可以根據這個demo了解象FlashGet之類的軟體是如何快速有效的分析出可以下載下傳的位址的。 

正規表達式的初體驗:分析字元串資料

 string url = "Http://news.sina.com.cn";

正規表達式的初體驗:分析字元串資料

 System.Net.WebRequest request = System.Net.WebRequest.Create(url);

正規表達式的初體驗:分析字元串資料

 System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();

正規表達式的初體驗:分析字元串資料

 System.IO.Stream httpStream = response.GetResponseStream();

正規表達式的初體驗:分析字元串資料

 System.IO.StreamReader htmlRead = new System.IO.StreamReader(httpStream);

正規表達式的初體驗:分析字元串資料

 string html = htmlRead.ReadToEnd();

正規表達式的初體驗:分析字元串資料
正規表達式的初體驗:分析字元串資料

 //System.Console.WriteLine(html); 

9

正規表達式的初體驗:分析字元串資料

10

正規表達式的初體驗:分析字元串資料

11

正規表達式的初體驗:分析字元串資料

 string pattern = @"http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?";

12

正規表達式的初體驗:分析字元串資料

13

正規表達式的初體驗:分析字元串資料

 System.Text.RegularExpressions.MatchCollection matches = rx.Matches(html);

14

正規表達式的初體驗:分析字元串資料

15

正規表達式的初體驗:分析字元串資料

 for (int i = 0; i <= matches.Count - 1; i++)

16

正規表達式的初體驗:分析字元串資料

17

正規表達式的初體驗:分析字元串資料

 System.Console.WriteLine(matches[i].Value);

18

正規表達式的初體驗:分析字元串資料

運作結果是,顯示所有連接配接,大緻如下

http://www.nen.com.cn/ target=_blank

http://www.tianshannet.com.cn/ target=_blank

http://www.nmgcb.com.cn/ target=_blank

http://www.jxcn.cn/ target=_blank

http://www.szed.com/ target=_blank

http://www.hljdaily.com.cn/ target=_blank

http://www.hifly.tv/ target=_blank

http://www.chinalegalnews.com.cn/ target=_blank

http://www.newssc.org

http://www.sconline.com.cn/ target=_blank

http://www.xawb.com/gb/news/node_2.htm target=_blank

http://www.anhuinews.com/ target=_blank

http://www.xinminweekly.com.cn/ target=_blank

http://news.cnhubei.com/ target=_blank

http://www.sdnews.com.cn/ target=_blank

http://www.qingdaonews.com/ target=_blank

http://www.dzwww.com/ target=_blank

http://www.66wz.com/cmsweb/webportal/ target=_blank

http://www.bjd.com.cn/ target=_blank

http://www.beijing.org.cn

http://news.sina.com.cn/media.html target=_blank

http://image2.sina.com.cn/c.gif width=1 height=1

http://tech.sina.com.cn/focus/sinahelp.shtml target=_blank

http://net.china.cn/chinese/index.htm target=_blank

http://corp.sina.com.cn/chn/ class=sinatail

http://corp.sina.com.cn/eng/ class=sinatail

http://ads.sina.com.cn/ class=sinatail

http://www.sina.com.cn/contactus.html class=sinatail

http://corp.sina.com.cn/chn/sina_job.html class=sinatail

http://www.sina.com.cn/intro/lawfirm.shtml class=sinatail

http://english.sina.com

http://members.sina.com.cn/apply/ class=sinatail

http://tech.sina.com.cn/focus/sinahelp.shtml class=sinatail

http://www.sina.com.cn/intro/copyright.shtml class=sinatail

http://ad4.sina.com.cn/200702/13/82610_news-couple-l.swf

http://ad4.sina.com.cn/200702/13/82609_news-couple-r.swf

http://ad4.sina.com.cn/sina/ae/ad_src/couplet/coupletv4.js

http://rm.sina.com.cn/icast/rotator.js

http://cast.icast.com.cn/a/4/4/1/9/1/389.js

http://image2.sina.com.cn/unipro/pub/getclickinfo_sinahome.js

本文轉自shyleoking 51CTO部落格,原文連結:http://blog.51cto.com/shyleoking/806259

繼續閱讀