split string by spaces properly accounting for quotes and backslashes (ruby)

regex, ruby

Solution

I think I found simple enough solution: `input.scan(/(?:"(?:\\.|[^"])*"|[^" ])+/)`

Problem

I want to split a string (insecure foreign line, like exim_mainlog line) by spaces, but not by spaces that are inside of double quotes, and ignore if the quote is escaped by a backslash like `\"`, and ignore the backslash if it is just escaped like `\\`. Without slow parsing the string manually with FSM. Example line: `U=mailnull T="test \"quote\" and wild blackslash\\" P=esmtps` Should be split into: `["U=mailnull", "T=\"test \\\"quote\\\" and wild blackslash\\\"", "P=esmtps"]` (Btw, I think ruby should had method for such split.., sigh).

Original source