以下の内容はhttps://white-azalea.hatenablog.jp/entry/20110412/1302621853より取得しました。


正規表現で特定の要素をぶっこぬく。

教材は、、、ウチのサイトのRSSでいいか。

http://d.hatena.ne.jp/white-azalea/rss

ここからタイトルとリンクを正規表現でぶっこぬく。
正規表現の書き方講座する気はないので、要は抽出置換しか考えない。

<title>(?<title>.+)</title>\s+<link>(?<url>http://[\w_\?\-\./]+)</link>

URL の認識はかなり適当。

実際にコードに食わす場合こんな感じに。

Regex regex = new Regex(@"<title>(?<title>.+)</title>\s+<link>(?<url>http://[\w_\?\-\./]+)</link>");

// allString がRSS読み取ったヤツだとおもいねぇ
MatchCollection matches = regex.Matches(allString);

foreach (Match match in matches)
{
    Console.WriteLine(
        match.Result("title:${title}, url:${url}"));
}

後方参照するだけであれば、

Regex regex = new Regex(@"<title>(.+)</title>\s+<link>(http://[\w_\?\-\./]+)</link>");
MatchCollection matches = regex.Matches(allString);

foreach (Match match in matches)
{
    Console.WriteLine(
        "title:{0}, url:{1}",
        match.Groups[1], match.Groups[2]);
}

でも

foreach (Match match in matches)
{
    Console.WriteLine(
        match.Result(@"title:$1, url:$2"));
}

でも問題はない。




以上の内容はhttps://white-azalea.hatenablog.jp/entry/20110412/1302621853より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14