![](/img/trans.png)
[英]Regex pattern extract string between curly braces and exclude curly braces
[英]Regex pattern to get string between curly braces
我有一个字符串The quick brown {fox, dragon, dinosaur} jumps over the lazy {dog, cat, bear, {lion, tiger}}.
我想得到大括号之间的所有字符串。 必须忽略花括号内的花括号。 PHP数组中的预期输出将是
[0] => fox, dragon, dinosaur
[1] => dog, cat, bear, {lion, tiger}
我尝试了这种模式\\{([\\s\\S]*)\\}
来自花花括号之间的正则表达式模式提取字符串,并排除 Mar回答的花括号 ,但似乎这个模式在花括号之间得到所有字符串而不分割不相关的文本(不确定正确的用词)。 这是上面模式的输出
fox, jumps, over} over the lazy {dog, cat, bear, {lion, tiger}}
打印上述句子的预期输出的最佳正则表达式模式是什么?
您可以在PHP中使用此递归正则表达式模式:
$re = '/( { ( (?: [^{}]* | (?1) )* ) } )/x';
$str = "The quick brown {fox, dragon, dinosaur} jumps over the lazy {dog, cat, bear, {lion, tiger}}.";
preg_match_all($re, $str, $matches);
print_r($matches[2]);
正如anubhava所说,你可以使用递归模式来做到这一点。
但是,他的版本非常“慢”,并未涵盖所有情况。
我个人使用这个正则表达式:
#({(?>[^{}]|(?0))*?})#
正如你在那里看到的那样: http ://lumadis.be/regex/test_regex.php?id = 2516它的速度更快; 并匹配更多结果。
那么它是怎样工作的?
/
( # capturing group
{ # looks for the char '{'
(?> # atomic group, engine will never backtrack his choice
[^{}] # looks for a non-'{}' char
| # or
(?0) # re-run the regex in a subroutine to match a subgroup
)*? # and does it as many time as needed
} # looks for the char '}'
) # ends the capture
/x
添加'?' '*'让它变得非贪婪。 如果你在那里使用贪婪的量词,那么引擎将启动比使用ungreedy的子程序更多的子程序。 (如果您需要更多解释,请告诉我)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.