开发者

Tokenize a command string

开发者 https://www.devze.com 2022-12-28 21:36 出处:网络
I have string like this: command \". / * or any other char like this\" some_param=\"string param\" some_param2=50

I have string like this:

command ". / * or any other char like this" some_param="string param" some_param2=50

I want to tokenize this string into:

command
". / * or any other char like this"
some_param="string param"
some_param2=50

I know it's possible to split with spaces but these parameters can also be seperated by commas, like:

command ". / * or any other char like this", some_param="string开发者_开发知识库 param", some_param2=50

I tried to do it like this:

\w+\=?\"?.+\"?

but it didn't work.


The stdlib module shlex is designed for parsing shell-like command syntax:

>>> import shlex
>>> s = 'command ". / * or any other char like this" some_param="string param" some_param2=50'
>>> shlex.split(s)
['command', '. / * or any other char like this', 'some_param=string param', 'some_param2=50']

The only difference from your desired result is that the quoted string is returned as the string value, not as the quoted string literal.


Something like this?

>>> x='command ". / * or any other char like this" some_param="string param" some_param2=50'
>>>
>>> re.findall('\w+\=\d+|\w+\="[^"]+"|"[^"]+"|\w+',x)
['command', '". / * or any other char like this"', 'some_param="string param"', 'some_param2=50']
>>>
0

精彩评论

暂无评论...
验证码 换一张
取 消