$matches Array: ( [0] => Array ( [0-6ren">
gpt4 book ai didi

php - 如何在正则表达式环视中匹配多个(N)空格?

转载 作者:行者123 更新时间:2023-12-04 04:56:40 24 4
gpt4 key购买 nike

我有一个正则表达式 (?<={% start %}).*?(?={% end %})匹配两个自定义标签之间的所有内容。

问题是,如果标签内有空格(例如,“{% start %}”),我添加 \s+?条件,正则表达式失败。以下代码不起作用:(?<={%\s+?start\s+?%}).*?(?={%\s+?end\s+?%})我在 PHP 中遇到错误:

preg_match_all(): Compilation failed: lookbehind assertion is not fixed length at offset 25

如果我删除前瞻/后视,同样的正则表达式会起作用: ({%\s+?(start|end)\s+%}) .

请指教。

最佳答案

说明

试试这个 permlink

[{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}]

这将匹配您 {% 中的所有文本和 %}括号,并将在将值放入其组之前自动修剪文本。

Group 0 获取整个匹配字符串
  • 获取开始标签文本
  • 获取内部文本
  • 获取结束标签文本

  • enter image description here

    免责声明

    这可能会有一些边缘情况,如果您将复杂数据嵌套到 sub 中,则正则表达式将失败,如果是这样,那么使用正则表达式可能不是执行此任务的最佳工具。

    总结
    [{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}]
    Char class [{] matches one of the following chars: {
    % Literal `%`
    \s 0 to infinite times [lazy] Whitespace [\t \r\n\f]
    \b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
    1st Capturing group ([^}]*start[^}]*)
    Negated char class [^}] infinite to 0 times matches any char except: }
    start Literal `start`
    Negated char class [^}] infinite to 0 times matches any char except: }
    \b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
    \s 0 to infinite times [lazy] Whitespace [\t \r\n\f]
    % Literal `%`
    Char class [}] matches one of the following chars: }
    \s 0 to infinite times [lazy] Whitespace [\t \r\n\f]
    \b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
    2nd Capturing group (.*?)
    . 0 to infinite times [lazy] Any character (except newline)
    \b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
    \s 0 to infinite times [lazy] Whitespace [\t \r\n\f]
    Char class [{] matches one of the following chars: {
    % Literal `%`
    \s infinite to 0 times Whitespace [\t \r\n\f]
    \b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
    3rd Capturing group ([^}]*end[^}]*)
    Negated char class [^}] infinite to 0 times matches any char except: }
    end Literal `end`
    Negated char class [^}] infinite to 0 times matches any char except: }
    \b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
    \s infinite to 0 times Whitespace [\t \r\n\f]
    % Literal `%`
    Char class [}] matches one of the following chars: }

    PHP 示例

    带有示例文本 {% start %} this is a sample text 1 {% end %}{% start %} this is a sample text 2 {% end %}
    <?php
    $sourcestring="your source string";
    preg_match_all('/[{]%\s*?\b([^}]*start[^}]*)\b\s*?%[}]\s*?\b(.*?)\b\s*?[{]%\s*\b([^}]*end[^}]*)\b\s*%[}]/i',$sourcestring,$matches);
    echo "<pre>".print_r($matches,true);
    ?>

    $matches Array:
    (
    [0] => Array
    (
    [0] => {% start %} this is a sample text 1 {% end %}
    [1] => {% start %} this is a sample text 2 {% end %}
    )

    [1] => Array
    (
    [0] => start
    [1] => start
    )

    [2] => Array
    (
    [0] => this is a sample text 1
    [1] => this is a sample text 2
    )

    [3] => Array
    (
    [0] => end
    [1] => end
    )

    )

    关于php - 如何在正则表达式环视中匹配多个(N)空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16670613/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com