正则表达式.txt

来自「VS自带教程,菜单[帮助]/目录/Visual Studio .NET/Visu」· 文本 代码 · 共 59 行

TXT
59
字号
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.emay.net.cn 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);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?