Php 正则表达式以匹配除2个连续大括号以外的任何内容
除了两个连续的大括号({)之外,正则表达式还能匹配什么 示例字符串:Php 正则表达式以匹配除2个连续大括号以外的任何内容,php,regex,pcre,Php,Regex,Pcre,除了两个连续的大括号({)之外,正则表达式还能匹配什么 示例字符串: {{some text}}string我想要{{另一个集合{{和内部}}}} 我只想得到我想要的字符串 我曾想过使用stack来做这些事情,但我想知道是否可以使用正则表达式来完成。 我正在使用PHP的PCRE 提前感谢使用前瞻断言(?!{{{}}})来验证外部集合中没有嵌套的大括号集合 {{((?!{{|}}).)*}} 测试程序 它似乎也能合理处理各种边缘情况: # Unbalanced braces. string(
{{some text}}string我想要{{另一个集合{{和内部}}}}
我只想得到我想要的
字符串
我曾想过使用stack来做这些事情,但我想知道是否可以使用正则表达式来完成。
我正在使用PHP的PCRE
提前感谢使用前瞻断言(?!{{{}}})
来验证外部集合中没有嵌套的大括号集合
{{((?!{{|}}).)*}}
测试程序
它似乎也能合理处理各种边缘情况:
# Unbalanced braces.
string(23) "{{lot {{of}} characters"
string(17) "{{lot characters"
string(23) "lot {{of}} characters}}"
string(17) "lot characters}}"
# Multiple sets of braces.
string(25) "{{lot }}of{{ characters}}"
string(2) "of"
# Lone curlies.
string(41) "{{lot {{of {single curly} }} characters}}"
string(19) "{{lot characters}}"
string(0) ""
如果需要对内容执行更复杂的操作,例如处理内容或变量,那么可以使用递归regexp,使用(?R)运算符
$data = "{{abcde{{fg{{hi}}jk}}lm}}";
$regexp = "#\{\{((?:[^(\{\{)(\}\})]+|(?R))+)\}\}#";
$count = 0;
function revMatch($matches) {
global $regexp, $count;
if (is_array($matches)) {
// Match detected, process for nested components
$subData = preg_replace_callback($regexp, 'revMatch', $matches[1]);
} else {
// No match, leave text alone
$subData = $matches;
}
// This numbers each match, to demonstrate call order
return "(" . $count++ . ":<" . $subData . ">)";
}
echo preg_replace_callback($regexp, 'revMatch', $data);
$data=“{{abcde{{fg{{{hi}}}jk}}lm}”;
$regexp=“\{((?:[^(\{)(\}})]+\\{(?R))+)\}};
$count=0;
函数revMatch($matches){
全局$regexp,$count;
if(is_数组($matches)){
//检测到匹配,嵌套组件的进程
$subData=preg_replace_回调($regexp,'revMatch',$matches[1]);
}否则{
//不匹配,请保留文本
$subData=$matches;
}
//这将对每个匹配项进行编号,以演示呼叫顺序
返回“(“$count++.”:)”;
}
echo preg_replace_回调($regexp,'revMatch',$data);
这将:{{abcde{{fg{{hi}}}jk}}}
转换为(2:)
关于regexp的一点解释:{{((?:[^({\{)(\}})]+{124;(?R))+)\}}
前面和后面的双大括号与任何目标构件匹配,大括号的内容是两个定义选项中的一个或多个:
不带双大括号的字符串[^(\{\{)(\}\})]+
整个regexp重复执行。(?:)
括号是一个非捕获组
注意,#s
是模式分隔符,我认为额外的斜杠会进一步降低可读性。对于{不平衡的{{大括号}
,预期的结果是什么?让我们假设没有不平衡的大括号。我从未听说过的(;)
魔法是什么?@metro-这是另一种编写while(true)的方式
。
# Unbalanced braces.
string(23) "{{lot {{of}} characters"
string(17) "{{lot characters"
string(23) "lot {{of}} characters}}"
string(17) "lot characters}}"
# Multiple sets of braces.
string(25) "{{lot }}of{{ characters}}"
string(2) "of"
# Lone curlies.
string(41) "{{lot {{of {single curly} }} characters}}"
string(19) "{{lot characters}}"
string(0) ""
$data = "{{abcde{{fg{{hi}}jk}}lm}}";
$regexp = "#\{\{((?:[^(\{\{)(\}\})]+|(?R))+)\}\}#";
$count = 0;
function revMatch($matches) {
global $regexp, $count;
if (is_array($matches)) {
// Match detected, process for nested components
$subData = preg_replace_callback($regexp, 'revMatch', $matches[1]);
} else {
// No match, leave text alone
$subData = $matches;
}
// This numbers each match, to demonstrate call order
return "(" . $count++ . ":<" . $subData . ">)";
}
echo preg_replace_callback($regexp, 'revMatch', $data);