从包含内容的字符串中删除 HTML 个标签

Remove HTML tags from a String with content

我有一个string = "195121<span class="up">+432</span>"。我需要正则表达式来删除标签及其内容(结果 string = "195121"

您可以尝试以下基于捕获组的正则表达式。

string.replaceAll("(?s)<(\w+)\b[^<>]*>.*?</\1>", "");

对我有用的主要正则表达式如下;它会删除具有给定标签名称的所有内容。

"(?is)<your_tag_name[^>]+>.*?<\/your_tag_name>"

我就是这样管理的。希望对其他人有帮助。

var data = "<p>Dhaka is the capital city of Bangladesh " +
    "and many palaces and mosques remain. This is" +
    " fast-growing modern metropolis.</p>\r\n<p>&lt;flightnode to=\"CXB\"&gt;&lt;/flightnode&gt;</p>"

首先将<>替换为<和>

// This replacement not needed if it's already been there
data = data.replace("&lt;", "<").replace("&gt;", ">")

然后打印并检查。

println("\n\n $data")

> //output //-> <p>Dhaka is the capital city of Bangladesh and many
> palaces and mosques remain. This is fast-growing modern
> metropolis.</p><p><flightnode to="CXB"></flightnode></p>

设置要删除的标签数组及其元素;

val tag = arrayOf("flightnode", "hotelnode ", "packagenode")

然后遍历你的字符串

for (value in tag) {
    val patternString = "(?is)<$value[^>]+>.*?<\/$value>"
    val pattern = compile(patternString)
    val matcher = pattern.matcher(data)
    println("\n\n" + matcher.find())
    data = matcher.replaceAll("")
}

打印出来查看。

println("\n\n" + data)

> // output // -> <p>Dhaka is the capital city of Bangladesh and many
> palaces and mosques remain. This is fast-growing modern
> metropolis.</p>\r\n<p></p>

感谢我的 ex-colleague @masud-bappy 创建正则表达式。