首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

纯正则表达式

2013-04-20 
求一个纯正则表达式本帖最后由 kqs42 于 2012-09-02 23:15:04 编辑比如一个字符串p111111p222222ABCp33333

求一个纯正则表达式
本帖最后由 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>

热点排行