作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
$matches Array: ( [0] => Array ( [0-6ren">
我有一个正则表达式 (?<={% 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*%[}]
{%
中的所有文本和
%}
括号,并将在将值放入其组之前自动修剪文本。
[{]%\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: }
{% 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/
我想在基于 DFA 的正则表达式匹配器中实现“词边界”匹配。谁能告诉我这是怎么做到的? 为了提供一些背景知识,我目前正在使用“dk.brics.automaton”库,但它不支持断言(例如 \b,字边
我是一名优秀的程序员,十分优秀!