Java 从html字符串中删除一些段落

Java 从html字符串中删除一些段落,java,android,regex,Java,Android,Regex,我有一个字符串,它是一系列html段落,我想删除(使用string的方法replaceAll)包含单词“UPDATE”的段落,通常它们的形式如下: <p><a href="blabla">(UPDATE)<a></p> 你能帮我找到“regex”部分吗?我对regex一点也不在行…我想这就是你要找的。您需要使用。*?而不是。*因为这迫使搜索变得懒惰而不是贪婪 public class Test { public static void

我有一个字符串,它是一系列html段落,我想删除(使用string的方法replaceAll)包含单词“UPDATE”的段落,通常它们的形式如下:

<p><a href="blabla">(UPDATE)<a></p>

你能帮我找到“regex”部分吗?我对regex一点也不在行…

我想这就是你要找的。您需要使用。*?而不是。*因为这迫使搜索变得懒惰而不是贪婪

public class Test {

    public static void main(String[] args) {
        String haystack = "<p><a href='bla'>(UPDATE)</a></p><p><a href='bla'><strong>(UPDATE)</strong></a></p><p><a href='bla'><strong>(Non uppercase 'update' to show this match is exact)</strong></a></p><p><a href='bla'><strong>This does not contain the word you're looking for</strong></a></p>";
        String regex = "<p>.*?(UPDATE).*?</p>";

        String result = haystack.replaceAll(regex, "");
        System.out.println("Result: " + result);
    }
}
公共类测试{
公共静态void main(字符串[]args){
字符串haystack=“”;
字符串regex=“*?(更新)。*?

”; 字符串结果=haystack.replaceAll(regex,“”); System.out.println(“结果:+Result”); } }
我想这就是你要找的。您需要使用。*?而不是。*因为这迫使搜索变得懒惰而不是贪婪

public class Test {

    public static void main(String[] args) {
        String haystack = "<p><a href='bla'>(UPDATE)</a></p><p><a href='bla'><strong>(UPDATE)</strong></a></p><p><a href='bla'><strong>(Non uppercase 'update' to show this match is exact)</strong></a></p><p><a href='bla'><strong>This does not contain the word you're looking for</strong></a></p>";
        String regex = "<p>.*?(UPDATE).*?</p>";

        String result = haystack.replaceAll(regex, "");
        System.out.println("Result: " + result);
    }
}
公共类测试{
公共静态void main(字符串[]args){
字符串haystack=“”;
字符串regex=“*?(更新)。*?

”; 字符串结果=haystack.replaceAll(regex,“”); System.out.println(“结果:+Result”); } }
强烈反对使用正则表达式,并建议使用轻量级HTML解析器(如tagsoup)来执行此操作。它适用于android应用程序,因为我只有这些字符串。您认为添加外部.jar文件更好吗?不过,我更担心性能…如果你说我会有所改进,我可以试试…强烈反对regex,建议使用tagsoup这样的轻量级HTML解析器。这是针对android应用程序的,因为我只有那些字符串,你认为添加外部.jar文件更好吗?不过,我更担心表现……如果你说我会有进步,我可以试试……对不起,到底有什么区别?效率更高吗?@Phate Jeroen regex更好,因为如果你一段接一段地写两段,就会发现两段都有+1对于Jeroen,我删除了我的。对不起,确切的区别在哪里?效率更高吗?@Phate Jeroen regex更好,因为如果你有两段,一段接一段,两段都会找到+1对于Jeroen,我删除了我的。