如何从java中的字符串中删除一些单词 [英] how to remove some words from a string in java
本文介绍了如何从java中的字符串中删除一些单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 android 平台上工作,我使用一个字符串变量来填充 html 内容,然后我想删除一些单词(特别是 - 删除 ..</head> 之间的任何单词;
标签.有什么解决办法吗?
im working on android platform,I use a string variable to fill the html content after that i want to delete some words(Specifically- delete whatever words are there in between <head>..</head>
tag. Any solution?
推荐答案
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中的字符串中删除一些单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文