新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:[C#][正则表达式]寻找匹配的Groups的几种方法

[C#][正则表达式]寻找匹配的Groups的几种方法

新客网 XKER.COM 2004-09-23 来源: 收藏本文
寻找匹配的Groups的几种方法示例:

//
// 两种大方法:
// MatchCollection<->Matches
// Match<->Match方式
//
// 第一大种:
MatchCollection mMCollection =
oRegex.Matches(strHTMLContent);
if(mMCollection.Count > 1)
{
foreach(Match m in mMCollection)
{
Group ghiddentonecodes = m.Groups["hiddentonecodes"];
strValue = ghiddentonecodes.Value;
}
}

// 第二大种:
// 这里面有两种方式:
// 第2.1种:NextMacth方式
Match mNext;
int posn, length;
for ( mNext = oRegex.Match( strHTMLContent ) ; mNext.Success ; mNext = mNext.NextMatch() )
{
foreach( Group g in mNext.Groups )
{
if( g.Length != 0 )
{
// Position of Capture object.
posn = g.Index;
// Length of Capture object.
length = g.Length;
strValue = g.Value;
}
}
}
//
// 第2.2种:CaptureCollection方式
////String[] results = new String[20];
// Loop through the match collection to retrieve all
// matches and positions.
Match mResult = oRegex.Match(strHTMLContent);
if(false == mResult.Success)
{
m_strLastError =
("[ParseFile][解析HTML]错误描述:没有匹配到");
return "";
}
CaptureCollection cc;
foreach(Group g in mResult.Groups)
{
// Capture the Collection for Group(i).
cc = g.Captures;
for (int j = 0; j < cc.Count; j++)
{
// Position of Capture object.
posn = cc[j].Index;
// Length of Capture object.
length = cc[j].Length;
strValue = cc[j].Value;
}
}


收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐