Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

ruby

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).

like image 415
catpnosis Avatar asked Feb 18 '23 17:02

catpnosis


1 Answers

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

like image 140
catpnosis Avatar answered Feb 21 '23 07:02

catpnosis