我需要准备一个可用于删除bbcode的正则表达式[QUOTE] (示例:[QUOTE = name]; [QUOTE = 123; 123])标记,当它嵌套在预先存在的[QUOTE]标记内时。
我正在使用的当前正则表达式来自此处的答案(包含正则表达式的完整说明):Remove nested quotes
~\G(?!\A)(?>(\[quote\b[^]]*](?>[^[]+|\[(?!/?quote)|(?1))*\[/quote])|(?<!\[)(?>[^[]+|\[(?!/?quote))+\K)|\[quote\b[^]]*]\K~
但是,这只会删除第二个或后来的嵌套[QUOTE]标记,但会留下一个嵌套标记,如果它们嵌套在现有的[QUOTE]标记中,我现在想要删除所有[QUOTE]标记。
输入的示例以及它应如何从preg_replace()
输出:
//input
$string = 'Here is a quote [QUOTE=person]I am also quoting [QUOTE]this is a nested quote[/QUOTE][/QUOTE]';
//preg replace
$string = preg_replace('regular_expression', '', $string);
//output
echo $string;
// Here is a quote [QUOTE=person]I am also quoting[/QUOTE]
答案 0 :(得分:0)
您可以使用递归方法:
(\[QUOTE[^][]*\])
(
(?:[^][]+|(?R))+
)
\[/QUOTE\]
<小时/>
在PHP
中,这将是:
$regex = '~
(\[QUOTE[^][]*\])
(
(?:[^][]+|(?R))+
)
(\[/QUOTE\])
~x';
$string = 'Here is a quote [QUOTE=person]I am also quoting [QUOTE]this is a nested quote[/QUOTE][/QUOTE]';
$string = preg_replace_callback($regex,
function($match) {
return $match[1] . preg_replace('~\[/?[^][]*\]~', '', $match[2]) . $match[3];
},
$string);
echo $string;
?>
<小时/> 这产生了
Here is a quote [QUOTE=person]I am also quoting this is a nested quote[/QUOTE]