I am trying to parse strings that look like shell commands. The general structure of these command is the following:
command value -arg1name arg1val -arg2name arg2val ... -argMname argMval
Here is an example,
abc cmdh1521 -x 123 -y sadg -zzz 563sd
I am using the Python re module to parse, search and group strings so that I get an output like this,
(command, value, ((-arg1name, arg1val), (arg2name, arg2val), ... (argMname, argMval))
I tried the following set of commands, but my output is not what I want it to be.
import re
cmd = "abc cmdh1521 -x 123 -y sadg -zzz 563sd"
_parser = r"^([a-z]+)\s{1}(\S*)((\s+\-[a-z]+\s{1}\S+)*)"
out = re.search(_parser, cmd)
print out.groups()
Here is the output I get
('abc', 'cmdh1521', ' -x 123 -y sadg -zzz 563sd', ' -zzz 563sd')
What am I doing wrong?
I can easily implement a non-regex solution, but I would like to know if there is a regex that can give me the kind of parsing I want?