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

本文介绍了使用正则表达式进行HTML内容匹配的多种方法,包括MatchCollection与Matches的运用,以及通过NextMatch方式和CaptureCollection来定位和获取所需信息的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

寻找匹配的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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值