in reply to Splitting up quoted/escaped command line arguments
State machine. Crawl the string character by character, keeping track of things like if you opened with a single quote, last saw an equals sign or backslash... Start out with a for (split //) {..., and stash the characters on a buffer. The buffer could be either an independent scalar or $args[-1], depending on taste.
Regular expression with backreferences. This is more challenging, because regular expressions aren't really intended to split up an entire string, but rather grab substrings. Expressions like "[^"]*(?<!\\)" to grab everything between two unescaped double quotes could be helpful, but remember if the command were echo "He said, \"How are you?\"", the intended output from your process would be ($command, @args) = ('echo', 'He said, "How are you?"'), which requires removing the surrounding quotes as well as unescaping.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Splitting up quoted/escaped command line arguments
by Tommy (Chaplain) on Feb 11, 2014 at 18:57 UTC | |
by Tommy (Chaplain) on Feb 11, 2014 at 19:31 UTC | |
by choroba (Cardinal) on Feb 11, 2014 at 20:46 UTC | |
by Tommy (Chaplain) on Feb 11, 2014 at 21:25 UTC | |
by choroba (Cardinal) on Feb 11, 2014 at 21:27 UTC | |
|