Php preg_match只捕获特定的短语

Example string:

$string = 'text text text text {capture this1, this2} text text text text';

I want to use preg_match, to get only these this1, and this2 in an array and nothing else.

I was trying something like so:

$array = array();
$reg = '/\{capture.*\}/'; //this needs changing
preg_match($reg, $string, $array);
var_dump($array);

This captures {capture this1, this2}. How do i exclude the '{' sign from regex? I was trying something like .!\{ but it gave me errors. Any suggestions?

You can try with following regex:

/\{(capture[^}]*)\}/

You can use this:

$pattern = '~(?:{capture |\G(?<!\A), )\K[^,}]+(?=(})?)~';
$data = 'text text text {capture this1, this2} text text';
$tmp = array();

if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
    foreach($matches as $m) {
        $tmp[] = $m[0];
        if (isset($m[1])) {
            $result = $tmp;
            break; 
        }
    }
}
print_r($result);

This pattern avoid to use this lookahead (?=[^}]*}) to check the closing curly bracket presence and use (?=(})?) that is faster to test. if the capture group exists, you know that the brackets are closed.

However I think that Shankar Damodaran method is more simple (and possibly more efficient).