Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split on white spaces not between quotes? [duplicate]

I am trying to split a string on white spaces only (\s), but that are not between a "quoted" section.

I am matching all text in between these quoted sections in the following manner:

(['"`]).*?\1

Regex101

However, when I try to add this as a negative lookahead, to only split on white spaces outside of those quotes, I can't get it to work:

\s(?!(['"`]).*?\1)

Regex101

How can I only split on the white spaces that are not in "quotes"?

like image 875
KevBot Avatar asked Nov 08 '16 05:11

KevBot


1 Answers

\s(?=(?:[^'"`]*(['"`])[^'"`]*\1)*[^'"`]*$)

You can use this regex with lookahead to split upon.See demo.

https://regex101.com/r/5I209k/4

or if mixed tick types.

https://regex101.com/r/5I209k/7

like image 128
vks Avatar answered Nov 14 '22 23:11

vks