简体   繁体   中英

How to replace varying text from a String

I have this HTML portion presented to me in a String.

<h1 id="Table1">Hello And welcome</h1>
<h1 id="Table2">Hello And welcome</h1>
<h1 id="Table3">Hello And welcome</h1>
<h1 id="Table4">Hello And welcome</h1>

I am trying to remove the id="*" attribute from the above string. So that the final String should contain only this:

<h1>Hello And welcome</h1>
<h1>Hello And welcome</h1>
<h1>Hello And welcome</h1>
<h1>Hello And welcome</h1>

I am using the replaceAll() method, but am unable to construct a regex to do so. Please advice.

String result = subject.replaceAll("<h1 id=\"[^\"]*\">", "<h1>");

应该适用于这种简单的情况。

    String s = 
                    "<h1 id=\"Table1\">Hello And welcome</h1>"+
                    "<h1 id=\"Table2\">Hello And welcome</h1>"+
                    "<h1 id=\"Table3\">Hello And welcome</h1>"+
                    "<h1 id=\"Table4\">Hello And welcome</h1>";
    System.out.println(s.replaceAll("\\sid=\".*?\"", ""));

output

<h1>Hello And welcome</h1><h1>Hello And welcome</h1><h1>Hello And welcome</h1><h1>Hello And welcome</h1>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM