C#正则表达式去除XML标签

时间:2015-12-16 18:48:20   收藏:0   阅读:756

案例1:

//数据源

String strSource = "<Sample>xxx<Extract>100</Extract></Sample> 11 <Extract>100<Extract>";

 

//表达式

String matchpattern = @"<([^>]*)>(.*?)<\/\1>";

//$2=(.*?) 进行替换

String replacementpattern = @"$2";

//循环判断 是否还有正确的XML标签
while (Regex.IsMatch(strSource, matchpattern))
{
strSource = Regex.Replace(strSource, matchpattern, replacementpattern, RegexOptions.IgnoreCase);
}

//输出结果:

//xxx100 11 <Extract>100<Extract>

案例2:

//标签中带属性

String strSource = "<Sample Name=‘Sample ‘>xxx<Extract>100</Extract></Sample> 11 <Extract>100<Extract>";

//表达式

String matchpattern = @"<(\w+)([^>]*)>(.*?)<\/\1>";

 

原文:http://www.cnblogs.com/Harvard-L/p/5051803.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!