C#正则表达式中MatchCollection类的用法
2009-11-11 21:54
281 查看
MatchCollection 类表示成功的非重叠匹配的只读的集合,MatchCollection 的实例是由 Regex.Matches 属性返回的,下面的实例说明了通过在输入字符串中找到所有与Regex中指定的匹配并填充 MatchCollection。
1、MatchCollection mc;
Code
public static string GetUrl(string text)
{
string pattern = @"(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])";
MatchCollection matchs;
matchs = Regex.Matches(text,pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
foreach (Match m in matchs)
{
text = text.Replace(m.ToString(), "<a target=\"_new\" href=\"" + m.ToString() + "\">" + m.ToString() + "</a>");
}
return text;
}
Mark.
1、MatchCollection mc;
Code
public static string GetUrl(string text)
{
string pattern = @"(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])";
MatchCollection matchs;
matchs = Regex.Matches(text,pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
foreach (Match m in matchs)
{
text = text.Replace(m.ToString(), "<a target=\"_new\" href=\"" + m.ToString() + "\">" + m.ToString() + "</a>");
}
return text;
}
Mark.
相关文章推荐
- C#正则表达式编程(三):Match类和Group类用法 .
- c#正则表达式用法举例
- c#正则表达式用法大全
- C#正则表达式之Regex类用法详解
- C#操作字符串以及正则表达式的用法总结
- C#正则表达式编程(二):Regex类用法
- C#正则表达式编程(三):Match类和Group类用法
- C#正则表达式编程(二):Regex类用法 .
- c#正则表达式的用法
- (转)C#正则表达式Regex类的用法
- C# 正则表达式的用法
- C#正则表达式编程(二):Regex类用法
- C#正则表达式编程(三):Match类和Group类用法
- C#正则表达式replace用法
- c#正则表达式常用用法
- C# 正则表达式的用法 及 常用正则表达式
- C#正则表达式编程(二):Regex类用法
- C#正则表达式的简单用法
- C#正则表达式编程(二):Regex类用法
- C#正则表达式Regex类的用法