如果不匹配则忽略第二个匹配的 HTML

发布于 2024-12-05 14:41:48 字数 746 浏览 0 评论 0 原文

我正在编写一个应该执行以下操作的正则表达式:

== 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 为空,我想忽略

部分。

欢迎任何其他提示/改进,我想学习:)

I'm writing a regular expression that should do the following:

== Text ==
Other text
==     Text==

Becomes

<h2>Text</h2>
<p>Other text</p>
<h2>Text</h2>

I'm almost there, the problem is that this is what I currently get:

<h2>Text</h2>
<p>Other text</p>
<h2>Text</h2>
<p></p>

Even though it's unlikely the heading will not be followed by text, I want to fix it at least for learning purposes.

Here is my function:

preg_replace('/== *(.*?) *==([^=]*)/m', 
             '<h2>$1</h2>
              <p>$2</p>
             ', '== Text ==
                 Other text
                 ==     Text==');

So basically, I want to ignore the <p></p> part if $2 is empty.

Any other tips / improvements are welcomed, I want to learn :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-12 14:41:48

您需要一个简单的条件来防止出现空的

标记。虽然我通常不建议这样做,但插入这个简单 if 的最简单方法是使用 /e 正则表达式修饰符 preg_replace

preg_replace('/== *(.*?) *==([^=]*)/me', 
             '"<h2>$1</h2>".(trim("$2") == ""?"":"<p>$2</p>")',
             '== Text ==
                 Other text
              ==     Text==');

此修饰符使替换字符串在进行替换之前被评估为 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 simple if is by using the /e regex modifier to preg_replace:

preg_replace('/== *(.*?) *==([^=]*)/me', 
             '"<h2>$1</h2>".(trim("$2") == ""?"":"<p>$2</p>")',
             '== Text ==
                 Other text
              ==     Text==');

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.

任性一次 2024-12-12 14:41:48

分两步完成此操作如何:

首先在不以 == 开头/结尾的每一行周围添加段落标记:

$firststep = preg_replace('/^(?![ \t]*==.*==[ \t]*$).+/m', '<p>\0</p>', $subject);

然后在以 = 开头/结尾的每一行周围添加标题标记=

$result = preg_replace('/^[ \t]*==[ \t]*(.*?)[ \t]*==[ \t]*$/m', '<h2>\1</h2>', $firststep);

How about doing this in two steps:

First add paragraph markers around each line that doesn't begin/end with ==:

$firststep = preg_replace('/^(?![ \t]*==.*==[ \t]*$).+/m', '<p>\0</p>', $subject);

Then add heading markers around each line that does begin/end with ==:

$result = preg_replace('/^[ \t]*==[ \t]*(.*?)[ \t]*==[ \t]*$/m', '<h2>\1</h2>', $firststep);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文