求一个正则表达式
<span style="w233">1234</span>456<span style="w233">789</span>想把所有的span去掉,得到1234456789. --------------------编程问答--------------------
string str=@"<span style=""w233"">1234</span>456<span style=""w233"">789</span>";--------------------编程问答-------------------- 一楼这个好象不行啊,只能匹配<span>,</span>好象没有去掉啊 --------------------编程问答--------------------
string result = Regex.Replace(str,@"</?span[^>]*?>","");
Console.WriteLine(result);
匹配<span>,</span>;然后替换为"" --------------------编程问答-------------------- 我测试木有问题啊
你把你的代码帖上来看下。 --------------------编程问答--------------------
--------------------编程问答--------------------
string tempStr =@"<span style=""w233"">1234</span>456<span style=""w233"">789</span>";
string pattern = @"<[^>]+>";
string tempStr1 = Regex.Replace(tempStr, pattern, "");//输出: 1234456789
你的代码,得到1234</span>456</span>789</span> --------------------编程问答-------------------- 不知道你怎么测试的。
--------------------编程问答-------------------- 突然发现我真的该去看看正则表达式了,每次要什么正则表达式都是去网上找现成的,但是总是不满足需求,奶奶的 --------------------编程问答--------------------
理事兄 的完全可以,我测试了。 --------------------编程问答--------------------
--------------------编程问答--------------------
string regtxt =@"<span style=""w233"">1234</span>456<span style=""w233"">789</span>";
Regex treg = new Regex(@"(?<txt>[^>]+?)[<]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Match tmc=treg.Match(regtxt);
while (tmc.Success)
{
Response.Write(tmc.Groups["txt"] );
tmc = tmc.NextMatch();
}
public string RemoveHTMLTags(string htmlStream)
{
if (htmlStream != "")
{
htmlStream = Regex.Replace(htmlStream, "(<script)[\\s\\S]*?(</script>)|(<style)[\\s\\S]*?(</style>)", " ", RegexOptions.IgnoreCase);
htmlStream = Regex.Replace(htmlStream, "<[^>]+>| |&|| ||•|<|>", " ", RegexOptions.IgnoreCase);
htmlStream = Regex.Replace(htmlStream, "(\r\n[\r|\n|\t| ]*\r\n)|(\n[\r|\n|\t| ]*\n)", "\r\n");
htmlStream = Regex.Replace(htmlStream, "[\t| ]{1,}", " ");
}
return htmlStream.Trim();
}
试试这方法 --------------------编程问答--------------------
--------------------编程问答--------------------
Regex ContentRegex = new Regex("<[^<>]+>?", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
我加了这个才行的
result = Regex.Replace(result,@"<\/?span[^>]*?>","");
补充:.NET技术 , ASP.NET