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']
>>>
精彩评论