java - 如何从java中的字符串中删除一些单词

标签 java android string replace

我在 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/

相关文章:

android - Android API 7 (2.1 Eclair)下Camera的字符串参数

java - NotifyDataSetChanged() 不刷新列表?

java - imageView 在 Nexus 5 (Android 6.0) 中不显示

java - Java中String CompareTo函数的时间复杂度是多少?

java - 在jsp中打印 session 属性

java - 创造更亮的颜色? ( java )

java - 我如何在这个名词查找程序中处理这个ArrayIndexOutOfBoundException

java - 当我对 List 进行排序时,它的迭代器会发生什么?

string - 在字符串中查找字母 (charAt)

javascript - Jquery 将整数转换为字符串并返回