我得到了带有以下字符串的变量$randomWiki:
[...]"wgNamespaceNumber":0,"wgPageName":"Busfahrer","wgTitle":"Busfahrer","wgCurRevisionId"[...]
并希望从中获取"wgTitle"。我构建了以下正则表达式:
preg_match_all("/'"wgTitle'":'"(.*?)'"/", $randomWiki);
var_dump给了我布尔(假)。我已经使用在线工具检查了正则表达式并且它有效。
有什么建议吗?
你应该使用以下代码:
$str = '"wgNamespaceNumber":0,"wgPageName":"Busfahrer","wgTitle":"Busfahrer","wgCurRevisionId"';
preg_match_all("#'"wgTitle'":'"([^'"]*)'"#", $str, $res);
var_dump($res);
preg_match_all接受 3 个参数。
试试这个,
'"wgTitle'":'"('w)*'"
具有肯定断言的解决方案?=
:
$str = '"wgNamespaceNumber":0,"wgPageName":"Busfahrer","wgTitle":"Busfahrer","wgCurRevisionId"';
// it matches the word followed by separator ," - which is the boundary of the next key
preg_match_all("/'"wgTitle'":'"('w+)'"(?=,'")/iu", $str, $matches);
var_dump($matches[1]);
// the output:
array(1) {
[0]=>
string(9) "Busfahrer"
}
http://php.net/manual/en/regexp.reference.assertions.php