我在 android 平台上工作,我使用一个字符串变量来填充 html 内容,之后我想删除一些单词(特别是删除 <head>..</head>
标签之间的任何单词。有什么解决方案吗?
最佳答案
String newHtml = oldHtml.replaceFirst("(?s)(<head>)(.*?)(</head>)","$1$3");
解释:
oldHtml.replaceFirst(" // we want to match only one occurrance
(?s) // we need to turn Pattern.DOTALL mode on
// (. matches everything, including line breaks)
(<head>) // match the start tag and store it in group $1
(.*?) // put contents in group $2, .*? will match non-greedy,
// i.e. select the shortest possible match
(</head>) // match the end tag and store it in group $3
","$1$3"); // replace with contents of group $1 and $3
关于java - 如何从java中的字符串中删除一些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186066/