教材は、、、ウチのサイトの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")); }
でも問題はない。