I have this HTML portion presented to me in a String.
我将这个HTML部分以字符串形式呈现给我。
<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:
我试图从上面的字符串中删除id =“*”属性。这样最终的String应该只包含这个:
<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.
我正在使用replaceAll()方法,但我无法构造一个正则表达式。请指教。
2 个解决方案
#1
2
String result = subject.replaceAll("<h1 id=\"[^\"]*\">", "<h1>");
should work for this simple scenario.
应该适用于这个简单的场景。
#2
1
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>
#1
2
String result = subject.replaceAll("<h1 id=\"[^\"]*\">", "<h1>");
should work for this simple scenario.
应该适用于这个简单的场景。
#2
1
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>