正则表达式的初体验:分析字符串数据
2007-02-16 22:39
120 查看
在程序中,对于数据的分析是非常重要及常见的。对于字符串数据的分析,往往就是对字符串的内容提取,捕获和替换。除非你要做的分析是简单的要求,否则你就不要再使用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
修正了正则表达式,又可以对数据有其他的分析
1
string pattern = @"^[-|+]?/d+(/./d{0,2})?$";
2
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(pattern);
3
string[] patternTest =
{ "-12.23", "+12.23", "22.3", "22", "=2.34", "2.34.45", "32. ","343.343", "" };
4
for (int i = 0; i <= patternTest.Length - 1; i++)
5
{
6
System.Console.WriteLine("{0} is {1}", patternTest[i], rx.IsMatch(patternTest[i]));
7
}
运行结果是
-12.23 is True
+12.23 is True
22.3 is True
22 is True
=2.34 is False
2.34.45 is False
32. is False
343.343 is False
is False
依据这个例子,可以作一个很有效的分析用户输入的程序了
以下是一个向远程请求http页面的,根据返回的结果,查找url连接的demo,可以根据这个demo了解象FlashGet之类的软件是如何快速有效的分析出可以下载的地址的。
1
string url = "Http://news.sina.com.cn";
2
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
3
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
4
System.IO.Stream httpStream = response.GetResponseStream();
5
System.IO.StreamReader htmlRead = new System.IO.StreamReader(httpStream);
6
string html = htmlRead.ReadToEnd();
7
8
//System.Console.WriteLine(html);
9
10
11
string pattern = @"http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?";
12
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(pattern);
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
正则表达式虽然学习起来非常的另人头痛和难过,但他也实在是非常的有效。
.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
修正了正则表达式,又可以对数据有其他的分析
1
string pattern = @"^[-|+]?/d+(/./d{0,2})?$";
2
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(pattern);
3
string[] patternTest =
{ "-12.23", "+12.23", "22.3", "22", "=2.34", "2.34.45", "32. ","343.343", "" };
4
for (int i = 0; i <= patternTest.Length - 1; i++)
5
{
6
System.Console.WriteLine("{0} is {1}", patternTest[i], rx.IsMatch(patternTest[i]));
7
}
运行结果是
-12.23 is True
+12.23 is True
22.3 is True
22 is True
=2.34 is False
2.34.45 is False
32. is False
343.343 is False
is False
依据这个例子,可以作一个很有效的分析用户输入的程序了
以下是一个向远程请求http页面的,根据返回的结果,查找url连接的demo,可以根据这个demo了解象FlashGet之类的软件是如何快速有效的分析出可以下载的地址的。
1
string url = "Http://news.sina.com.cn";
2
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
3
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
4
System.IO.Stream httpStream = response.GetResponseStream();
5
System.IO.StreamReader htmlRead = new System.IO.StreamReader(httpStream);
6
string html = htmlRead.ReadToEnd();
7
8
//System.Console.WriteLine(html);
9
10
11
string pattern = @"http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?";
12
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(pattern);
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
相关文章推荐
- 正则表达式的初体验:分析字符串数据
- 正则表达式的初体验:分析字符串数据
- 字符串处理是许多程序中非常重要的一部分,它们可以用于文本显示,数据表示,查找键和很多目的.在Unix下,用户可以使用正则表达式的强健功能实现这些 目的,从Java1.4起,Java核心API就引入了java.util.regex程序包,它是一种有价值的基础
- 正则表达式应用:分析查询字符串
- 黑马程序员_特殊的对象---字符串 、正则表达式、基本数据类型
- javascript 性能分析:字符串和正则表达式
- ES6学习笔记之正则表达式和字符串正则方法分析
- Python数据分析学习-re正则表达式模块
- 正则表达式分割字符串但是不分割字符串中引号中间的数据,处理CSV文件必备!!
- [置顶] 正则表达式应用:分析查询字符串
- 使用正则表达式抽取字符串中数据的样例
- 递归性高或者逻辑简单、性能要求严格、维护人数多的字符串分析,尽量避免正则表达式
- oracle 正则表达式 ( 前端传入连续字符串分析..)
- Python 3(4)数据类型:字符串 & 正则表达式
- 判断字符串是否为数字函数,正则表达式【C#】
- 字符串中的正则表达式特殊符号 转
- Java正则表达式:不包含某些字符串 怎么写?
- 高效中英文字符串截取方法[不用普遍的正则表达式方法]
- 正则表达式,验证身份证,手机号,电话号码,邮箱,邮编,日期,正负数,英文字符串等
- C++ - 正则表达式 的 子表达式(subexpressions) 的 数据验证(data validation)