比如:class="g">topic.csdn.net/u/20091018/11/ef331a56-93f ... 2011-3-28 </span> - <a其中以class="g">开头 ;</span> - <a结尾如何获取中间的字符串:topic.csdn.net/u/20091018/11/ef331a56-93f ... 2011-3-28
string str = "class="g">topic.csdn.net/u/20091018/11/ef331a56-93f ... 2011-3-28 </span> - <a"; Regex reg = new Regex(@"(?s)(?<=class="g">).*(?=</span> - <a)"); foreach (Match m in reg.Matches(str)) { Response.Write(m.Value + ""); }//topic.csdn.net/u/20091018/11/ef331a56-93f ... 2011-3-28
应多加个?,如下:
@"(?s)(?<=class="g">).*?(?=</span> - <a)"原因是str一般是网页的源码,里面相同的内容比较多,避免匹配多余的内容!