读书人

纯正则表达式

发布时间: 2013-04-20 19:43:01 作者: rapoo

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

p111111p222222ABCp333333p444444

匹配字符串ABC的前一个p内的字符串和后一个p内的字符串

要求匹配结构是:p222222ABCp333333
[解决办法]
p\d+ABCp\d+
[解决办法]

 string test = @"xxxxx<br />     两性:<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 />     两性:<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;
//不包含两边(中间部分):    两性:<a href=""http://sex.fh21.com.cn/"" target=""_blank"">http://sex.fh21.com.cn/</a>

读书人网 >C#

热点推荐