代码之家  ›  专栏  ›  技术社区  ›  Marcos Placona

用换行符替换<p>标记的正则表达式

  •  3
  • Marcos Placona  · 技术社区  · 16 年前

    我有下面的HTML

    <p>Some text <a title="link" href="http://link.com/" target="_blank">my link</a> more 
    text <a title="link" href="http://link.com/" target="_blank">more link</a>.</p>
    <p>Another paragraph.</p>
    <p>[code:cf]</p>
    <p>&lt;cfset ArrFruits = ["Orange", "Apple", "Peach", "Blueberry", </p>
    <p>"Blackberry", "Strawberry", "Grape", "Mango", </p>
    <p>"Clementine", "Cherry", "Plum", "Guava", </p>
    <p>"Cranberry"]&gt;</p>
    <p>[/code]</p>
    <p>Another line</p>
    <p><img src="http://image.jpg" alt="Array" />
    </p>
    <p>More text</p>
    <p>[code:cf]</p>
    <p>&lt;table border="1"&gt;</p>
    <p> &lt;cfoutput&gt;</p>
    <p> &lt;cfloop array="#GroupsOf(ArrFruits, 5)#" index="arrFruitsIX"&gt;</p>
    <p>  &lt;tr&gt;</p>
    <p> &lt;cfloop array="#arrFruitsIX#" index="arrFruit"&gt;</p>
    <p>     &lt;td&gt;#arrFruit#&lt;/td&gt;</p>
    <p> &lt;/cfloop&gt;</p>
    <p>  &lt;/tr&gt;</p>
    <p> &lt;/cfloop&gt;</p>
    <p> &lt;/cfoutput&gt;</p>
    <p>&lt;/table&gt;</p>
    <p>[/code]</p>
    <p>With an output that looks like:</p>
    <p><img src="another_image.jpg" alt="" width="342" height="85" /></p>
    

    <p> 或 </p> 每当它发现 < ,它将替换为换行符。

    /\<p\>(.*?)(<\/p>)/g
    

    $1\n
    

    这一切看起来不错,但它也取代了内部的内容 [code][/code] < <p> 不是吗 内部 [code] 标签。

    \<p\>^\[code*\](.*?)(<\/p>)
    

    但显然这不管用:-)

    谁能帮我一下这个正则表达式吗?

    提前谢谢

    3 回复  |  直到 15 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    我假设您对生成要解析的HTML的应用程序有特别的了解,否则 you would not be even considering regular expressions for the task . (我想,其中一部分也是知识 <p> 标记总是出现在换行符之后 </p> 结束标记总是出现在换行符之前。)

    如上所述,仅使用正则表达式无法轻松或高效地实现您想要实现的目标(必须使用复杂的嵌套的look behind和look ahead断言来验证 <p>...</p> 事件不在 [code]...[/code] 块和非固定长度的look-behind断言特别有限,而IIRC在JDK1.6之前是一个普通的bug。)

    code 和非- 块,并将块传输到输出序列中 代码 <p></p> 代码 (大块)

    代码 大块。

        2
  •  1
  •   Will Iverson Arion    16 年前

    解析HTML的表达式。我是 完全意识到这一点,但仍然 正则表达式。

    你能再解释一下吗?

        3
  •  0
  •   Ming-Tang    16 年前

    否定先行的语法是 (?!) .

    (?![code.*?]([^\[]|\[\/[^c]|\[\/c[^o]|\[\/co[^d]|\[\/cod[^e]|\[\/code[^\]])*)<p>.*?</p>