首页 > 解决方案 > 为什么这个正则表达式只匹配模式的最后一次出现

问题描述

我正在尝试创建一个正则表达式,它将用标记代码创建 html。

当尝试替换 [table] 标记的一部分时,它只替换最后一次出现。

我有以下正则表达式(PHP):

/(\[table].*)\[\|](.*\[\/table])/s

替换模式:

$1</td><td>$2

以及以下测试字符串:

[table]<thead>
<th>head1</th><th>head2</th></thead>
[*]test1[|]test2
[*]test1[|]test2
[/table]

它应该产生以下内容:

[table]<thead>
<th>head1</th><th>head2</th></thead>
[*]test1</td><td>test2
[*]test1</td><td>test2
[/table]

但它实际上是这样的:

[table]<thead>
<th>head1</th><th>head2</th></thead>
[*]test1[|]test2
[*]test1</td><td>test2
[/table]

问题在于,它[|]在其他标记代码中使用但不应替换为</td><td>


澄清一下:我有一个表“bb-code”

[table]
[**]header1[||]header2[||]header3[||]...[/**]
[*]child1.1[|]child1.2[|]child1.3[|]...
[*]child2.1[|]child2.2[|]child2.3[|]...
[*]child3.1[|]child3.2[|]child3.3[|]...
[*]...[|]...[|]...[|]...
[/table]

我希望它变成这样:

<table class="ui compact stripet yellow table">
    <thead>
        <tr>
            <th>header1</th>
            <th>header2</th>
            <th>header3</th>
            <th>....</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>child1.1</td>
            <td>child1.2</td>
            <td>child1.3</td>
            <td>...</td>
        </tr>
        <tr>
            <td>child2.1</td>
            <td>child2.2</td>
            <td>child2.3</td>
            <td>...</td>
        </tr>
        <tr>
            <td>child3.1</td>
            <td>child3.2</td>
            <td>child3.3</td>
            <td>...</td>
        </tr>
    </tbody>
</table>

标签: phpregexpcre

解决方案


好的,我在睡前有几分钟的时间在手机上,所以我跑了 Wiktor 的评论并编写了一系列preg_函数来尝试将你的 bbcode 转换为 html。我对 bbcode 没有任何经验,所以我纯粹是针对您的示例输入而不考虑边缘情况。我认为 php 在某处有一个 bbcode 解析器库,但我不知道你的 bbcode 语法是否是标准的。

实现的模式的一些分解。

首先,隔离[table]...[/table]文档中的每个完整字符串。( Regex101 Demo )~\[table]\R*([^[]*(?:\[(?!/?table])[^[]*)*)\R*\[/table]~将匹配字符串并传递完整匹配 as$m[0]和表标签之间的子字符串 as $m[1]to BBTableToHTML()

接下来,BBTableToHTML()将对$m[1]字符串进行 3 次单独的传递。这些模式中的每一个都会将它们各自匹配的字符串发送到关联的自定义函数并返回修改后的字符串。

在将更新$m[1]BBTableToHTML()的内容发送到之前echo,您想要的<table...></table>标签将结束$m[1]

模式演示preg_replace_callback_array()

  1. ~\[\*\*]([^[]*(?:\[(?!/?\*\*])[^[]*)*)\[/\*\*]~ https://regex101.com/r/thINHQ/2
  2. ~(?:\[\*].*\R*)+~ https://regex101.com/r/thINHQ/3
  3. ~\[\*](.*)~ https://regex101.com/r/thINHQ/4

代码:(演示

$bbcode = <<<BBCODE
[b]Check out this demo[/b]
¯\_(ツ)_/¯
[table]
[**]header1[||]header2[||]header3[||]...[/**]
[*]child1.1[|]child1.2[|]child1.3[|]...
[*]child2.1[|]child2.2[|]child2.3[|]...
[*]child3.1[|]child3.2[|]child3.3[|]...
[*]...[|]...[|]...[|]...
[/table]
simple text
[table]
[**]a 1[||]and a 2[/**]
[*]A[|]B
[*]C[|]D
[/table]

[s]3, you're out[/s]
blah
BBCODE;

function BBTableToHTML($m) {
    return "<table class=\"ui compact stripet yellow table\">\n" .
           preg_replace_callback_array(
               [
                   '~\[\*\*]([^[]*(?:\[(?!/?\*\*])[^[]*)*)\[/\*\*]~' => 'BBTHeadToHTML',
                   '~(?:\[\*].*\R*)+~' => 'BBTBodyToHTML',
                   '~\[\*](.*)~' => 'BBTBodyRowToHTML'
               ],
               $m[1]
           ) .
           "</table>";
}

function BBTHeadToHTML($m) {
    return "\t<thead>\n" .
           "\t\t<tr>\n\t\t\t<th>" . str_replace('[||]', "</th>\n\t\t\t<th>", $m[1]) . "</th>\n\t\t</tr>\n" .
           "\t</thead>";
}

function BBTBodyToHTML($m) {
    return "\t<tbody>\n{$m[0]}\t</tbody>\n";
}

function BBTBodyRowToHTML($m) {
    return "\t\t<tr>\n\t\t\t<td>" . str_replace('[|]', "</td>\n\t\t\t<td>", $m[1]) . "</td>\n\t\t</tr>";
}

echo preg_replace_callback(
         '~\[table]\R*([^[]*(?:\[(?!/?table])[^[]*)*)\R*\[/table]~',
         'BBTableToHTML',
         $bbcode
     );

输出:

[b]Check out this demo[/b]
¯\_(ツ)_/¯
<table class="ui compact stripet yellow table">
    <thead>
        <tr>
            <th>header1</th>
            <th>header2</th>
            <th>header3</th>
            <th>...</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>child1.1</td>
            <td>child1.2</td>
            <td>child1.3</td>
            <td>...</td>
        </tr>
        <tr>
            <td>child2.1</td>
            <td>child2.2</td>
            <td>child2.3</td>
            <td>...</td>
        </tr>
        <tr>
            <td>child3.1</td>
            <td>child3.2</td>
            <td>child3.3</td>
            <td>...</td>
        </tr>
        <tr>
            <td>...</td>
            <td>...</td>
            <td>...</td>
            <td>...</td>
        </tr>
    </tbody>
</table>
simple text
<table class="ui compact stripet yellow table">
    <thead>
        <tr>
            <th>a 1</th>
            <th>and a 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>A</td>
            <td>B</td>
        </tr>
        <tr>
            <td>C</td>
            <td>D</td>
        </tr>
    </tbody>
</table>

[s]3, you're out[/s]
blah

推荐阅读