正则 截取固定开头结尾字符串中间的字符串
比如: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 + "<br/><br/><br/><br/>"); } //topic.csdn.net/u/20091018/11/ef331a56-93f ... 2011-3-28
应多加个?,如下:
@"(?s)(?<=class="g">).*?(?=</span> - <a)"
原因是str一般是网页的源码,里面相同的内容比较多,避免匹配多余的内容!
posted on 2012-05-18 11:02 HOT SUMMER 阅读(6446) 评论(0) 编辑 收藏 举报