Regex用于在每个{space}上分割字符串,但当包含在引号中时不分割


Regex to split a string up on each {space} but not when contained in quotes

我不太擅长正则表达式(实际上根本不知道多少)。我需要创建一个数组从字符串分割在空格,但只有当空间不在双引号内,所以:

this.line "should be 3" elements

看起来像:

this.line
should be 3
elements

我知道我可以使用preg_match来获取数组,但我对正则表达式没有任何线索。

p。s我在堆栈溢出中看过其他解决方案,但正则表达式似乎不适合我的preg_match。

谢谢。

你可以试试:

preg_match_all('/"[^"]+"|'S+/', $s, $matches);

查看其在线运行情况:ideone