【发布时间】:2017-09-23 15:56:42
【问题描述】:
我有一个如下形式的字符串
$string = "This is {test} for [a]{test2} for {test3}.";
我想得到所有没有方括号前缀的大括号。因此,在上面的字符串中,我想得到{test} 和{test3},而不是[a]{test2}。
我在答案https://stackoverflow.com/a/977294/2311074 中发现,如果使用负前瞻,这可能是可能的。所以我尝试了
$regex = '/(?:(?!\[[^\}]+\])\{[^\}]+\})/';
echo preg_match_all($regex, $string, $matches) . '<br>';
print_r($matches);
但这仍然给了我所有三个大括号。
3
数组 ( [0] => 数组 ( [0] => {test} [1] => {test2} [2] => {test3} ) )
为什么这不起作用?
【问题讨论】:
-
@WiktorStribiżew 感谢您的详细回答。我正在赶上负面展望的话题。一旦我理解了你的答案,我会回复/投票。
-
有什么不清楚的请马上问——我会在线几个小时。