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

news/2024/7/1 18:30:17

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




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





http://www.niftyadmin.cn/n/3648860.html

相关文章

Apollo Boost简介

With as much as we’ve gone over creating APIs with GraphQL and Prisma in previous articles, we’ve never actually applied our custom backend to a client-side app. In this article, you’ll learn how to let your user interact with your backend through queri…

成就Android英才之路

著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。作者&#xff1a;王宇龙链接&#xff1a;http://www.zhihu.com/question/19759722/answer/29213925来源&#xff1a;知乎由于工作需要大量修改framework代码, 在AOSP(Android Open Source Proj…

如何设计架构?

axing&#xff08;转载自www.Linuxaid.com.cn&#xff09;  2003年05月04日 Part 1 层 层&#xff08;layer&#xff09;这个概念在计算机领域是非常了不得的一个概念。计算机本身就体现了一种层的概念&#xff1a;系统调用层、设备驱动层、操作系统层、CPU指令集。每个层都负…

baseActivity的封装——模板式设计模式

public abstract class BaseActivity extends AppCompatActivity {Overrideprotected void onCreate(Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);//初始化布局setContentView();// 一些特定的算法&#xff0c;子类基本都会使用的(比如butterkni…

使用Visual Studio Code和ESLint进行保存

We need style guides to write consistent, reusable and clean code. But when you have been working 10 hours a day on a project for the past 3 months it’s hard to notice an extra indentation in your code or a single quote instead of a double quote. 我们需要…

Git使用教程详解之一 Git起步

起步 本章介绍开始使用 Git 前的相关知识。我们会先了解一些版本控制工具的历史背景&#xff0c;然后试着让 Git 在你的系统上跑起来&#xff0c;直到最后配置好&#xff0c;可以正常开始开发工作。读完本章&#xff0c;你就会明白为什么 Git 会如此流行&#xff0c;为什么你应…

网络引擎与数据库相结合

结合之前两篇文章链式调用打造第三方的网络引擎 http://blog.csdn.net/qq_24675479/article/details/79277616 和 自己动手搭建数据库框架 http://blog.csdn.net/qq_24675479/article/details/79285849 首先逻辑处理:每次都会请求数据&#xff0c;但是为了保证用户体验&…

css伪类选择器_如何使用CSS:root伪类选择器

css伪类选择器Learn about the CSS :root pseudo-class selector, and how you might want to use it in your projects! 了解CSS :root伪类选择器&#xff0c;以及如何在项目中使用它&#xff01; The CSS :root pseudo-class selector is used to select the highest-level …