PHP pcre regex查找花括号的内容
我需要PHP pcre正则表达式来查找所有花括号的内容。。。 为什么这个正则表达式不起作用PHP pcre regex查找花括号的内容,php,regex,pcre,Php,Regex,Pcre,我需要PHP pcre正则表达式来查找所有花括号的内容。。。 为什么这个正则表达式不起作用 <?php $str = "test{it}test{/it}test"; $ret = preg_match_all("#\{.?\}#u", $str, $matches); print_r($matches); ?> 我希望$matches包含{it}和{/it}-但它是空的….我想这就是您要找的版本:{.*?}在之前有* $re = "/{.*?}/"
<?php
$str = "test{it}test{/it}test";
$ret = preg_match_all("#\{.?\}#u", $str, $matches);
print_r($matches);
?>
我希望
$matches
包含{it}
和{/it}
-但它是空的….我想这就是您要找的版本:{.*?}
在之前有*
$re = "/{.*?}/";
$str = "test{it}test{/it}test";
preg_match_all($re, $str, $matches);
?
=>0或1个随机字符。你的意思是*?
(零个或多个随机字符,ungreedy)?将正则表达式更改为*?
{.*?}
你不需要转义{
,我个人会使用/{[^}]+}/
,匹配左括号和右括号,并在左括号和右括号之间至少匹配一个非括号字符