Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for parsing a shell command like strings

Tags:

python

regex

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?

like image 601
siva82kb Avatar asked Jun 17 '26 22:06

siva82kb


1 Answers

In this case you will have to use positive lookbehind regex as shown below:

(?<=-)(\w+) ([\w\d]+) 

Description and example is at: Demo

like image 189
Rahul Avatar answered Jun 19 '26 12:06

Rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!