如果不匹配则忽略第二个匹配的 HTML
我正在编写一个应该执行以下操作的正则表达式:
== Text ==
Other text
== Text==
变得
<h2>Text</h2>
<p>Other text</p>
<h2>Text</h2>
我快到了,问题是这就是我当前得到的:
<h2>Text</h2>
<p>Other text</p>
<h2>Text</h2>
<p></p>
尽管标题后面不太可能没有文本,但我想将其修复为至少用于学习目的。
这是我的函数:
preg_replace('/== *(.*?) *==([^=]*)/m',
'<h2>$1</h2>
<p>$2</p>
', '== Text ==
Other text
== Text==');
所以基本上,如果 $2
为空,我想忽略 部分。
欢迎任何其他提示/改进,我想学习:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个简单的条件来防止出现空的
标记。虽然我通常不建议这样做,但插入这个简单
if
的最简单方法是使用/e
正则表达式修饰符preg_replace
:此修饰符使替换字符串在进行替换之前被评估为 PHP 代码,因此您可以轻松地在其中添加一个小条件。
查看实际操作。
另一种选择是使用
preg_replace_callback
< /a>,这实际上是相同的想法,只是您现在将代码编写为单独的函数。恕我直言,这是更好的方法,因为它可以使代码更清晰。最后一点,如果您打算添加更多格式选项,您可能需要考虑将解析分解为多个步骤,并可能一次处理一行,因为正则表达式并非设计用于处理这种处理。你可以强迫它达到一定程度,但很快它就会变得非常难以维护。
You need one simple conditional to prevent the empty
<p>
tag from appearing. While I would not recommend this usually, the easiest way to insert this simpleif
is by using the/e
regex modifier topreg_replace
:This modifier makes the replacement string be evaluated as PHP code before making the replacement, so you can fit a small conditional in there easily.
See it in action.
Another option would be to use
preg_replace_callback
, which is effectively the same idea only that you now write the code as a separate function. This is better IMHO because it makes for clearer code.As a final note, if you intend to add more formatting options you might want to consider breaking your parsing down into multiple steps and possibly processing one line at a time because regular expressions are not designed to handle this kind of processing. You can force it up to a point, but then it starts to become very unmaintainable very quickly.
分两步完成此操作如何:
首先在不以
==
开头/结尾的每一行周围添加段落标记:然后在以
= 开头/结尾的每一行周围添加标题标记= :
How about doing this in two steps:
First add paragraph markers around each line that doesn't begin/end with
==
:Then add heading markers around each line that does begin/end with
==
: