Regular expression to split on whitespace but ignore a double-quoted string

I know I am /[\s]+/'

separating the string with spaces. I'm trying to expand on this to ignore any spaces in double quotes. So I want to Hello World

be split but not"Hello World"

preg_split('/[\s]+/', $string)

is the expression I'm using in PHP.

+3


source to share


1 answer


You can use PCRE verbs (*SKIP)(*FAIL)

to tell the regular expression to skip certain parts of the expression. So:

".*?"(*SKIP)(*FAIL)|\s+

      

will skip double quotes. Here's a demo of regex101:



https://regex101.com/r/eBP67C/1/

You can read more about this here http://www.rexegg.com/regex-best-trick.html#pcrevariation .

+3


source







All Articles