zoukankan      html  css  js  c++  java
  • Regex.Match 方法

    Regex.Match 方法

          在输入字符串中搜索正则表达式的匹配项,并将精确结果作为单个 Match 对象返回。

          重载列表
          (1) 在指定的输入字符串中搜索 Regex 构造函数中指定的正则表达式匹配项。

                     [C#] public Match Match(string);

         (2) 从指定的输入字符串起始位置开始在输入字符串中搜索正则表达式匹配项。

                   [C#] public Match Match(string, int);

         (3) 在指定的输入字符串中搜索 pattern 参数中提供的正则表达式的匹配项。

                  [C#] public static Match Match(string, string);

         (4)   从指定的输入字符串起始位置开始在输入字符串中搜索具有指定输入字符串长度的正则表达式匹配项。

               [C#] public Match Match(string, int, int);

       (5)    在输入字符串中搜索 pattern 参数中提供的正则表达式的匹配项(匹配选项在 options 参数中提供)。

              [C#] public static Match Match(string, string, RegexOptions);

    二、应用举例

       1.下面的代码是为了取出网页中的Title属性

            Match TitleMatch = Regex.Match(fileContents, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline );

            filetitle = TitleMatch.Groups[1].Value;

         注意红色的1, Regex.Match方法得到的Groups的索引是从1开始的,而不是从0开始的

    2. 下面的代码是为了取出网页头部的"Content"属性

                  Match DescriptionMatch = Regex.Match( fileContents, "<META NAME="DESCRIPTION" CONTENT="([^<]*)">", RegexOptions.IgnoreCase | RegexOptions.Multiline );
                  filedesc = DescriptionMatch.Groups[1].Value;

    3. 下面的代码用来分解一个字符串

       string text = "One car red car blue car";
       string pat = @"(w+)s+(car)";
       // Compile the regular expression.
       Regex r = new Regex(pat, RegexOptions.IgnoreCase);
       // Match the regular expression pattern against a text string.
       Match m = r.Match(text);
       int matchCount = 0;
       while (m.Success)
       {
        Response.Write("Match"+ (++matchCount) + "<br>");
        for (int i = 1; i <= 2; i++)
        {
         Group g = m.Groups[i];
         Response.Write("Group"+i+"='" + g + "'" + "<br>" );
         CaptureCollection cc = g.Captures;
         for (int j = 0; j < cc.Count; j++)
         {
          Capture c = cc[j];
          Response.Write("Capture"+j+"='" + c + "', Position="+c.Index + "<br>");
         }
        }
        m = m.NextMatch();
       }

    运行结果如下:

    Match1
    Group1='One'
    Capture0='One', Position=0
    Group2='car'
    Capture0='car', Position=4
    Match2
    Group1='red'
    Capture0='red', Position=8
    Group2='car'
    Capture0='car', Position=12
    Match3
    Group1='blue'
    Capture0='blue', Position=16
    Group2='car'
    Capture0='car', Position=21

    string str = @"<tr style='HEIGHT: 24.55pt'>
      <td style='WIDTH: 149px' valign='top' width='78' bgcolor='#eaeaea'>
        <b>楼盘地址</b>
      </td>
      <td valign='top' colspan='3'>
        <input id='textfield' title='楼盘地址' style='WIDTH: 231px; HEIGHT: 28px' size='9' value='星河湾荟心园7栋3座302' name='楼盘地址'>
      </td>
      <td style='WIDTH: 121px' valign='top' width='78' bgcolor='#eaeaea'>
        <b>成交日期</b>
      </td>
      <td style='WIDTH: 140px' valign='top' width='238'>
        <input id='textfield' title='客源号' style='WIDTH: 95px; HEIGHT: 28px' value='2010.9.25' name='客源号'>
      </td>
    </tr>";
            string str2= Regex.Match(str, "<input id='textfield' .*>?").ToString();
            string str3 = Regex.Match(str2, "value='.*?'").ToString();
            Response.Write(str3);

  • 相关阅读:
    三种钱是花的越多,赚的越多
    程序员除去繁华,你的匠心何在?
    科目三考试
    药房托管
    文章标题
    【cocos2d-x 3.7 飞机大战】 决战南海I (八) 背景移动
    Android开发时经经常使用的LogUtil
    仿支付宝/微信的password输入框效果GridPasswordView解析
    hdoj 1518 Square 【dfs】
    mysql配置文件夹错误:在安装mysql 5.6.19 时运行cmake命令是出现CMake Error: The source directory does not appear to contai
  • 原文地址:https://www.cnblogs.com/guozhe/p/3419708.html
Copyright © 2011-2022 走看看