正则表达式的使用
/* 正则表达式的使用 */ using System.Text.RegularExpressions; namespace Frank { public class Test { //程序入口 public static void Main(string[] args) { const string myText = "abcdeafsgabcadefgaaabcdefagaabcdefgaa"; string pattern = "a"; MatchCollection myMatches = Regex.Matches(myText,pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture); foreach(Match item in myMatches) { System.Console.WriteLine(item.Index);//输出包含指定字符的位置索引 } System.Console.WriteLine("---------------------------------"); //查找以a开头以fg结尾,中是非空格的字符 的字符串 pattern = @"\ba\S*aa\b"; myMatches = Regex.Matches(myText,pattern,RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture); System.Console.WriteLine(myMatches[0].Success+"----"+myMatches.Count);//是否匹配成功 } } }