正規表現である文字で挟まれた、部分文字列を取得する方法

using System.Text.RegularExpressions; して

Regexを使う

using System;
using System.Net;
using System.Text.RegularExpressions;

class RegexMatch {
  static void Main() {

    string anchor =
      "<a href=\"(?<url>.*?)\".*?>(?<text>.*?)</a>";

    // @ITのトップページを取得
    WebClient wc = new WebClient();
    string html = wc.DownloadString("http://www.atmarkit.co.jp/");

    Regex re = new Regex(anchor, RegexOptions.IgnoreCase
                               | RegexOptions.Singleline);

    for (Match m = re.Match(html); m.Success; m = m.NextMatch()) {
      string url = m.Groups["url"].Value;
      string text = m.Groups["text"].Value;
      Console.WriteLine(url);
      Console.WriteLine(text);
    }
  }
}


ネタ元