I have a string = "195121<span class="up">+432</span>"
. I need regEx to remove tags with its content (result string = "195121"
)
您可以尝试以下基于捕获组的正则表达式。
string.replaceAll("(?s)<(\\w+)\\b[^<>]*>.*?</\\1>", "");
The main regex works for me are below; It removes all content with a given tag name.
"(?is)<your_tag_name[^>]+>.*?<\\/your_tag_name>"
I manage it this way. Hope it helps others.
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><flightnode to=\"CXB\"></flightnode></p>"
First replace < and > to < and >
// This replacement not needed if it's already been there
data = data.replace("<", "<").replace(">", ">")
Then print & check it.
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>
Set tags array you want to remove with its elements ;
val tag = arrayOf("flightnode", "hotelnode ", "packagenode")
Then loop throught your string
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("")
}
Print to check it.
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>
Thanks my ex-colleague @masud-bappy for creating regex.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.