当出现特定变量时,我需要将html
分成两部分。
以下是一个例子:
<h1>I can have any kind of text here</h1>
<p> </p>
<p><em><strong>When I see this variable :)</strong> I want to rebuild my html.</em></p>
最后我想要的是什么:
<h1>I can have any kind of text here</h1>
<p> </p>
<p><em><strong>When I see this</strong></em></p>
<p><em><strong> :)</strong> I want to rebuild my html.</em></p>
以下是我的想法(告诉我是否有更好的方法):
编辑示例2:
<ol>
<li>
<h2>I can have any kind of text here 1</h2>
</li>
<li>
<h2><strong>I can have any kind of text here 2 variable</strong></h2>
</li>
<li>
<h2><strong>I can have any kind of text here 3</strong></h2>
</li>
<li>
<h2>I can have any kind of text here 4</h2>
</li>
<li><em><strong>When I see this</strong></em></li>
</ol>
<p> </p>
...
<ol>
<li>
<h2>I can have any kind of text here 1</h2>
</li>
<li>
<h2><strong>I can have any kind of text here 2 variable </strong></h2>
</li>
</ol>
可变
<ol>
<li>
<h2><strong>I can have any kind of text here 3</strong></h2>
</li>
<li>
<h2>I can have any kind of text here 4</h2>
</li>
<li><em><strong>When I see this</strong></em></li>
</ol>
<p> </p>
例3:
<p><a href="test">I can have any kind of text of varaible here even clickable.</a></p>
...
<p><a href="test">I can have any kind of text of</a></p>
变通
<p><a href="test">here even clickable.</a></p>
答案 0 :(得分:1)
众所周知,您应该使用 html解析器而不是正则表达式。
无论如何,你可以使用一个简单的字符串replaceAll来做你想要的。如果你想使用正则表达式,你可以使用这样的东西:
String str = "<h1>I can have any kind of text here</h1>\n<p> </p>\n<p><em><strong>When I see this variable :)</strong> I want to rebuild my html.</em></p>\n";
str = str.replaceAll("variable", "</strong></em></p>\n<p><em><strong>");
System.out.println(str);
<强> Working regex 强>
<强> IdeOne demo 强>
输出
<h1>I can have any kind of text here</h1>
<p> </p>
<p><em><strong>When I see this </strong></em></p>
<p><em><strong> :)</strong> I want to rebuild my html.</em></p>