我從跨度的innerHTML刮一年值和值是在括號中是這樣的:正則表達式:如何逃脫在C#中的「(」元字符
<span class="year_type">(2009)</span><br>
我想要得到的今年的值,而括號,但我試圖逃離「(」字符時,得到了一些編譯器錯誤
我的模式:
const string yearPattern = "<span class=\"year_type\">\((?<year>.*?)\)</span>";
完整代碼:
const string yearPattern = "<span class=\"year_type\">\((?<year>.*?)\)</span>";
var regex = new Regex(yearPattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
Match match = regex.Match(data);
return match.Groups["year"].Value;
什麼是逃避()
感謝
反斜槓爲編譯器轉義字符串文字。爲了逃避它的正則表達式類,你需要另一個。 – Ken 2010-03-07 18:43:36
有一個很棒的免費圖書館,名爲Html Agility Pack。它將避免你的整個問題,讓你的代碼更易於編寫和閱讀,並且不太可能在HTML邊緣案例中突破。 http://www.codeplex.com/htmlagilitypack請不要用正則表達式解析HTML。 http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html – TrueWill 2010-03-07 20:05:56
謝謝TueWill--將它合併了一個小時前,並享受它。 Betters文檔有助於實現目標。使用基本的LINQ,但不能使用lambda表達式? – Chev 2010-03-09 10:59:24