【发布时间】:2016-05-15 11:03:06
【问题描述】:
好的,所以我整天都在尝试解决这个问题,在 PHP 中使用各种正则表达式。我有一个这样的字符串:
$str = "this is a(n) {item} made by {username}. Watch out for \\{escaped} items. This one is \\\\{notescaped}.";
我需要捕获被花括号包围的非转义组。我计划使用preg_split 将组中的字符串(包括大括号)拆分为一个数组,以便稍后用所需的信息填充空白。
所以这个结果应该是:
// After preg_split
array (
[0] => "this is a(n) ",
[1] => " made by ",
[2] => ". Watch out for {escaped} items. This one is \\",
[3] => "."
);
// Captured delimiters from preg_split
array(
[0] => "{item}",
[1] => "{username}",
[2] => "{notescaped}",
);
我知道如何使用基本的\{.*\} 简单地捕获组,但我需要确保它没有被转义。所以像 \\\\\\{escaped}(3 backslashes) 这样的东西仍然会逃脱它。大括号内的字符也可以是任何字符,没有特定的内容,也没有任何需要检查转义的内容。非常感谢您提供任何帮助!
【问题讨论】: