如何使用正则表达式提取字符串?

$str = (( test: 'object1', test2: 'object2', test3: 'object3' ))

Out of the above string, I want to extract the word object1.

So, I wrote following code, but it doesn't work.

<?php
  echo preg_replace("/^(.+)test:(.+)'(.+)'(.+)$/", "$3", $str);
?>

It printed "object3"

How can I fix this problem?

You can simply use this regex:

.*?test:\s*'(.+?)'.*

Your example:

$str = "(( test:   'object1',   test2:  'object2',    test3:   'object3' ))";
echo preg_replace("/.*?test:\s*'(.+?)'.*/", "$1", $str); //: object1