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.

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

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置