にちゃんねるのスレッド一覧はsubject.txtというファイルに記述がある
こんなかんじのものが数百行
このファイルを一行づつ読んで
を一気に抽出できたら便利だなと思った
完成品
public class ThreadInfomation { public long threadKey { get; set; } public string threadTitle { get; set; } public int resCount { get; set; } public ThreadInfomation(string line) { Regex re = new Regex(@"(?<threadKey>\d{10})\.dat\<\>(?<title>.*)\t.*\((?<resCount>\d{1,4})\)$"); Match match = re.Match(line); this.threadKey = long.Parse(match.Groups["threadKey"].Value); this.threadTitle = match.Groups["title"].Value; this.resCount = int.Parse(match.Groups["resCount"].Value); } }
正規表現 Groupクラス
()で囲んだ部分がグループ化される
マッチした部分文字列はMatch.Groupsプロパティでアクセスできる
スレキー抽出部分を例につかうとこう
Regex re = new Regex(@"(\d{10})\.dat"); Match match = re.Match(line); this.threadKey = long.Parse(match.Groups[1].Value);
インデックスの0は正規表現全体にマッチした部分が格納されている
名前付きグループ
(?<groupName>)とするとグループに名前をつけられる
スレキー抽出部分を例につかうとこう
Regex re = new Regex(@"(?<threadKey>\d{10})\.dat"); Match match = re.Match(line); this.threadKey = long.Parse(match.Groups["threadKey"].Value);
同じ要領でスレタイとレス数についても書いていって完成
なんとなく読みやすい気がする