日期:2014-05-17  浏览次数:20759 次

求一个纯正则表达式
本帖最后由 kqs42 于 2012-09-02 23:15:04 编辑
比如一个字符串
 
 p111111p222222ABCp333333p444444
 
 匹配字符串ABC的前一个p内的字符串和后一个p内的字符串
 
 要求匹配结构是:p222222ABCp333333

------解决方案--------------------
p\d+ABCp\d+
------解决方案--------------------
 string test = @"xxxxx<br /> &nbsp;&nbsp;&nbsp; 两性:<a href=""http://sex.fh21.com.cn/"" target=""_blank"">http://sex.fh21.com.cn/</a></p>ttttuuuuuu";
            string href = "http://sex.fh21.com.cn/";
            string pattern = @"(?i)<br\s*?/>(?<text>((?!</p>)[\s\S])*?<a[^>]*?href=(['""]?)" + href + @"\2[^>]*?>((?!</p>)[\s\S])*?)</p>";
            string result = Regex.Match(test, pattern).Value;
            //包含两边:<br /> &nbsp;&nbsp;&nbsp; 两性:<a href=""http://sex.fh21.com.cn/"" target=""_blank"">http://sex.fh21.com.cn/</a></p>
            string text = Regex.Match(test, pattern).Groups["text"].Value;
            //不包含两边(中间部分):&nbsp;&nbsp;&nbsp; 两性:<a href=""http://sex.fh21.com.cn/"" target=""_blank"">http://sex.fh21.com.cn/</a>