我们要分析网页,把整个网页内容作为匹配源,但是c#的正则表达式是默认以一行为单位的,使用 RegexOptions.Multiline 也没有实质性作用(它知识改变了^和$的行为)
只要在正则表达式前面加上(?s)这个选项即可
参考资料:http://hi.baidu.com/hetaoos/blog/item/7936a80faec513e9ab6457b8.html
string s = "......."; string r = @"(?s)(?i)导演:s*</span>s*<spans+class=""m_c"">s*<span><a.*?>(?<director>.*?)</a>"; string t = Regex.Match(s, r).Groups["director"]; Console.WriteLine(t); // (?s) 点号匹配一切字符(含换行符) // (?i) 进行不区分大小写的匹配 // (?<name>...) 命名捕获
Regex r = new Regex("(?s)(?i)id="__VIEWSTATE" value="(?<__VIEWSTATE>[^"]+)" />.+" + "<span id="lbinfo">(?<lbinfo>[^<]+)</span>.+" + "<span id="lbIP">(?<lbIP>[^<]+)</span>.+" + "<span id="lbuserid">(?<lbuserid>[^<]+)</span>.+" + "<span id="lbRealName">(?<lbRealName>[^<]+)</span>.+" + "<span id="lbRemainMoney">(?<lbRemainMoney>[^<]+)</span>.+" + "<span id="lbFreshTime">(?<lbFreshTime>[^<]+)</span>.+" + "<span id="LNowState">(?<LNowState>(开放|关闭)IP成功).+</span>.+" + "id="__EVENTVALIDATION" value="(?<__EVENTVALIDATION>[^"]+)" />", RegexOptions.Compiled); |