tags:

views:

59

answers:

2

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.

+2  A: 

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']
>>>
S.Mark
I tried to change your regex with adding '|\w+="{3}[^"]+"{3}' after that to handle paramname="""paramvalue""" type parameters too but it didn't work. That's strange.
pocoa
Okay I put that on the front, now it works! Thank you!
pocoa
@pocoa, I guess, you need to add that in front of others like `\w+="{3}[^"]+"{3}|\w+=\d+|\w+="[^"]+"|"[^"]+"|\w+`
S.Mark
ah ok, I was writing comment to mention same thing, while you did it work.
S.Mark
:)) Thanks a lot!
pocoa
You're welcome :-)
S.Mark
+3  A: 

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.

Ned Batchelder
Thanks, it's a nice tool. But it's not working when these parameters are separated by commas.
pocoa