剥去字符串中的html标签

y--*_*y-- 7 regex vb.net string replace

我有一个程序,我正在编写,应该从字符串中删除html标签.我一直在尝试替换所有以"<"开头并以">"结尾的字符串.这(显然是因为我在这里问这个)到目前为止还没有奏效.这是我尝试过的:

StrippedContent = Regex.Replace(StrippedContent, "\<.*\>", "")
Run Code Online (Sandbox Code Playgroud)

这只是返回原始字符串的随机部分.我也试过了

For Each StringMatch As Match In Regex.Matches(StrippedContent, "\<.*\>")
    StrippedContent = StrippedContent.Replace(StringMatch.Value, "")
Next
Run Code Online (Sandbox Code Playgroud)

哪个做了同样的事情(返回看起来像原始字符串的随机部分).有一个更好的方法吗?更好的我指的是一种有效的方式.

Ro *_* Mi 27

描述

这个表达式将:

  • 找不到任何标签并替换所有标签
  • 避免有问题的边缘情况

正则表达式: <(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*>

替换为:没有

在此输入图像描述

示范文本

注意鼠标悬停功能中的困难边缘情况

these are <a onmouseover=' href="NotYourHref" ; if (6/a>3) { funRotator(href) } ; ' href=abc.aspx?filter=3&prefix=&num=11&suffix=>the droids</a> you are looking for.

Imports System.Text.RegularExpressions
Module Module1
  Sub Main()
    Dim sourcestring as String = "replace with your source string"
    Dim replacementstring as String = ""
    Dim matchpattern as String = "<(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*>"
    Console.Writeline(regex.Replace(sourcestring,matchpattern,replacementstring,RegexOptions.IgnoreCase OR RegexOptions.IgnorePatternWhitespace OR RegexOptions.Multiline OR RegexOptions.Singleline))
  End Sub
End Module
Run Code Online (Sandbox Code Playgroud)

更换后的字符串

these are the droids you are looking for.
Run Code Online (Sandbox Code Playgroud)

  • 到目前为止,我见过这个主题真的是最好的正则表达式!+1 (2认同)