Regex to break a line into each space, but not be quoted

I am not very familiar with regex (I really don't know anything). I need to create an array from a string, separated by spaces, but only when the space is not enclosed in double quotes, therefore:

this.line "should be 3" elements 

will look like this:

 this.line should be 3 elements 

I know that I can use preg_match to get an array, but I don't have a hint in the regex.

ps I looked at other solutions in Stack Overflow, but the regex doesn't seem to work with my preg_match.

Thanks.

+4
source share
1 answer

You can try the following:

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

See how it works on the Internet: ideone

+6
source

Source: https://habr.com/ru/post/1379729/


All Articles