http://gskinner.com/RegExr/?355lr
它在regexr中工作正常。如果我将它插入到我的preg_match()中:
$reason = preg_match('/(?<=error\: )(.*?)(?=\; Request)/', $message);
它总是返回0.
我认为它与preg_match()期望格式化模式的方式有关吗?
答案 0 :(得分:1)
请尝试使用此正则表达式,因为您已经使用过&#39;?&#39;切换贪婪:
$reason = preg_match('/(?<=error\: )(.*)(?=\; Request)/', $message);`
答案 1 :(得分:1)
对我来说似乎没问题。检查$ message是否未以任何方式转义或与该regexr不同。
$message = 'Request have return error: Invalid email syntax; Request: {"jsonrpc":"2.0","method":"add_contact","params":["420a42ea1685c9a273f26378aa82081b",{"campaign":"nfzM","name":"Chris Allen","email":"eric","ip":"127.0.0.1","customs":[{"name":"phone","content":"123456"},{"name":"address","content":"123 Fake Street"},{"name":"city","content":"Chicago"},{"name":"state","content":"IL"},{"name":"zipcode","content":"0001"},{"name":"country","content":"United States"},{"name":"company_name","content":"Acme"},{"name":"store_url","content":"http:\/\/www.google.com"},{"name":"timestamp","content":"06.10.13"},{"name":"subscriber_ip","content":"127.0.0.1"}]}],"id":2};';
$reason = preg_match('/(?<=error\: )(.*?)(?=\; Request)/', $message);
var_dump($reason);
回馈:
int(1)
答案 2 :(得分:1)
我认为您的问题不在您的正则表达式中,请注意;
和:
不需要转义。不过我建议你用这个:
if (preg_match('~error: \K[^;]++(?=; Request)~', $message, $reason))
print_r($reason);
注意 $ reason
的位置