【发布时间】:2014-03-11 09:52:54
【问题描述】:
示例字符串:
$string = 'text text text text {capture this1, this2} text text text text';
我想使用preg_match,仅将这些this1 和this2 放在一个数组中,仅此而已。
我正在尝试这样的事情:
$array = array();
$reg = '/\{capture.*\}/'; //this needs changing
preg_match($reg, $string, $array);
var_dump($array);
这会捕获{capture this1, this2}。如何从正则表达式中排除“{”符号?我正在尝试类似.!\{ 的东西,但它给了我错误。有什么建议吗?
【问题讨论】:
-
所以你只需要
capture this1, this2? -
不,我需要
this1和this2,数组看起来像array('this1', 'this2')。 -
用一个表达式来做
{capture\s*\K[^,{}]+(?=[^}]*})|\G(?<!^),\s*\K[^,{}]+(?=[^}]*}),解释起来很痛苦。见demo :) -
谢谢 HamZa,好像它返回了我需要的东西,我现在将检查它:) 谢谢
-
@HamZa 你是巫师吗?