在php中用逗号和等号分隔的字符串,带转义

在php中用逗号和等号分隔的字符串,带转义,php,regex,pcre,Php,Regex,Pcre,我想像这样拆分一个字符串: colors = blue, green, yellow, kinda violet\, not sure,purple\=almost magenta 使用正则表达式,因此结果应该是 colors blue green yellow kinda violet\, not sure purple\=almost magenta 我已经尝试了几个小时,但没有成功地使用这种丑陋的结构(对于preg_match_all): 这(对于preg_split) 请解释一下我做

我想像这样拆分一个字符串:

colors = blue, green, yellow, kinda violet\, not sure,purple\=almost magenta
使用正则表达式,因此结果应该是

colors
blue
green
yellow
kinda violet\, not sure
purple\=almost magenta
我已经尝试了几个小时,但没有成功地使用这种丑陋的结构(对于preg_match_all):

这(对于preg_split)


请解释一下我做错了什么?

你需要消极的回顾:

$s='colors=蓝色、绿色、黄色、有点紫\,不确定,紫色\=几乎是洋红色';

$res=preg_split('/(?不是很优雅,但这应该可以实现preg_split的技巧

/\s*[=,^(\\,)^(\\=)]\s*/
/(\s*=\s*)|(\s*,\s*)|(\s*\\=\s*)|(\s*\\,\s*)/g

最重要的是,不要忘记全局匹配末尾的g。

非常感谢!你让我开心了:)阅读了关于断言的内容。pcre中不存在g修饰符,你是不是把它与JavaScript正则表达式混在一起了?:)
$s = 'colors = blue, green, yellow, kinda violet\, not sure,purple\=almost magenta';

$res = preg_split('/(?<!\\\\)[,=]\s*/', $s);

print_r($res);
/(\s*=\s*)|(\s*,\s*)|(\s*\\=\s*)|(\s*\\,\s*)/g