I want \"git log --format=\'(%h) %s\' --abbrev=7 HEAD\"
to be split into
[
\"git\",
\"log\",
\"--format=\'(%h) %s\'\",
\"--abbrev=7\",
\
As I understand, the idea is to split the string on contiguous spaces except where the spaces are part of a substring surrounded by single quotes. I believe this will work:
/(?:[^ ']*(?:'[^']+')?[^ ']*)*/
but invite readers to subject it to careful scrutiny.
demo
This regex can be made self-documenting by writing it in free-spacing mode:
/
(?: # begin a non-capture group
[^ ']* # match 0+ chars other than spaces and single quotes
(?: # begin non-capture group
'[^']+' # match 1+ chars other than single quotes, surrounded
# by single quotes
)? # end non-capture group and make it optional
[^ ']* # match 0+ chars other than spaces and single quotes
)* # end non-capture group and execute it 0+ times
/x # free-spacing regex definition mode
This obviously will not work if there are nested single quotes.
@n.'pronouns'm. suggested an alternative regex that also works:
/([^ "']|'[^'"]*')*/
demo