Regex r = new Regex("aaa");//定义正则表达式r,并新建匹配项
Match m = r.Match("123aaa456");//匹配结果
if (m.Success)//判断匹配是否成功
{
Console.WriteLine(m.Index);
}
else
{
Console.WriteLine("未成功匹配");
}
string pattern = "--(.+?)--";//匹配模式
string replacement = "($1)";//匹配成功后的替代字符串,方便继续下次匹配
string input = "He said--decisively--that the time--whatever time it was--had come.";
foreach (Match match in Regex.Matches(input, pattern))
{
string result = match.Result(replacement);
Console.WriteLine(result);
Console.WriteLine(match.Index);//匹配结果在input中的位置
}
string pat = "Asd";
Regex reg=new Regex(pat,RegexOptions.IgnoreCase);//新建匹配,不区分大小写
MatchCollection mat = reg.Matches("Asd,asd,aSd");//所有匹配存入集合中
foreach (Match ma in mat)
{
Console.WriteLine(ma);
Console.WriteLine("sdfgs::"+ma.Groups[0].Value);//可以通过下标访问集合中的值
}
string pattern = "--(.+?)--";//匹配模式
string replacement = "($1)";//匹配成功后的替代字符串,方便继续下次匹配
string input = "He said--decisively--that the time--whatever time it was--had come.";
foreach (Match match in Regex.Matches(input, pattern))
{
string result = match.Result(replacement);
Console.WriteLine(result);
Console.WriteLine(match.Index);//匹配结果在input中的位置
}
string pat = "Asd";
Regex reg=new Regex(pat,RegexOptions.IgnoreCase);//新建匹配,不区分大小写
MatchCollection mat = reg.Matches("Asd,asd,aSd");//所有匹配存入集合中
foreach (Match ma in mat)
{
Console.WriteLine(ma);
Console.WriteLine("sdfgs::"+ma.Groups[0].Value);//可以通过下标访问集合中的值
}