您的位置:首页 > 其它

正则表达式的使用

2013-11-30 16:20 162 查看




/*
正则表达式的使用
*/
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);//是否匹配成功

}
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: