douyinlai2169 2013-06-24 22:43
浏览 36
已采纳

Regex Mystery - 你能帮我添加这些选项吗?

The problem

My regex below for a PHP preg_replace works great

 '|(?<!</h2>)\s*
(?!<h2>)|'

However I would much rather the 'h2' part be a section which searches for any of 'h2|p|li|div' etc, just whatever it can find out of the choices I give it.

I have tried all sorts like

'|(?<!</(?:h2|p|li|div)>)\s*
(?!<(?:h2|p|li|div)>)|'

But it just will not seem to work.

This would be a great help if anyone can point me in the correct direction as I am working this out as an alternative to a problem I posted yesterday.

Extra notes

My aim for the code BTW is to FIND ALL LINE BREAKS ( ) that DO NOT have a /h2 or /p etc BEFORE the line break and DO NOT have a h2 or p etc AFTER the linebreak. So....

example
                        <- find this
example
                        <- find this
example
                        <- not this (as <h2> next starts line)
<h2>example
                    <- find this
example</h2>
                   <- not this (as </h2> ends this line)
example
                        <- find this

Many many thanks,

  • 写回答

1条回答 默认 最新

  • dongmo2324 2013-06-25 00:30
    关注

    I would just use this:

    ~<(h2|p|li|div)([^\>]+)?>([^\<]+)?</(h2|p|li|div)>~
    

    That accounts for stuff like class="xyz" in the opening tag.

    This will fix the regex you provided, but if you want to accomplish what you described at the bottom of your question, you'll probably need something more complicated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效