拆分时动态重建html标签

时间:2015-12-21 18:07:06

标签: java html regex tags

当出现特定变量时,我需要将html分成两部分。

以下是一个例子:

<h1>I can have any kind of text here</h1>
<p>&nbsp;</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>&nbsp;</p>
<p><em><strong>When I see this</strong></em></p>

<p><em><strong> :)</strong> I want to rebuild my html.</em></p>

以下是我的想法(告诉我是否有更好的方法):

  1. 拆分我的变量
  2. 通过正则表达式获取所有标签? (我只知道正则表达式告诉我真或假)
  3. 将所有标签放在堆叠上?
  4. 循环两次(数组拆分1和数组拆分2)
  5. 编辑示例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>&nbsp;</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>&nbsp;</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>
    

1 个答案:

答案 0 :(得分:1)

众所周知,您应该使用 html解析器而不是正则表达式

无论如何,你可以使用一个简单的字符串replaceAll来做你想要的。如果你想使用正则表达式,你可以使用这样的东西:

String str = "<h1>I can have any kind of text here</h1>\n<p>&nbsp;</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>&nbsp;</p>
<p><em><strong>When I see this </strong></em></p>
<p><em><strong> :)</strong> I want to rebuild my html.</em></p>