织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>.NET(C#)>

c#正则表达式的操作

落鹤生 发布于 2010-05-26 21:20 点击:次 
用C#来进行正则表达式的操作的源代码。
TAG:

using System.Text.RegularExpressions; 
string Text="The software ad,is MeTone, a,is very gaaad!,Your Need";
//一般
// string Pattern="is";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//转义,以n开头的单词
// string Pattern=@"\bn";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//转义,以e结尾的单词
// string Pattern=@"e\b";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//转义,以M开头,以e结尾,中间是任何数量不为空的字符,\S表示不是空白的字符,*任何数量
// string Pattern=@"\bM\S*e\b";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//转义,以T只能是总文本中的第一个字符
// string Pattern=@"^T";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//转义,以d只能是总文本中的第一个字符
// string Pattern=@"d$";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//转义,.是除以换行符\n以外的任何一个字符
// string Pattern=@"g.d";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
// Console.WriteLine(NextMatch.Index);
//转义,+可以重复一次或多次的前导字符
// string Pattern=@"ga+d";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//转义,?可以重复零次或多次的前导字符
// string Pattern=@"ga+d";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//转义,?可以重复零次或多次的前导字符
// string Pattern=@"\sa";
// MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
// foreach(Match NextMatch in Matches)
// Console.WriteLine(NextMatch.Index);
//提取网址
Text="I'found the URL is http://www.rosoo.net is very good";
string Pattern=@"\b(\S+)://(\S+)(?::(\S+))?\b";
MatchCollection Matches=Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
foreach(Match NextMatch in Matches)
Console.WriteLine(NextMatch); 

(wiseman)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201005/9510.html]
本文出处:博客园 作者:wiseman
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容