I have a situation where user can enter commands with optional key value pairs and value may contain spaces ..
here are 4 - different form user input where key and value are separated with = sign and values have space:
"cmd=create-folder name=SelfServe - Test ride"
"cmd=create-folder name=SelfServe - Test ride server=prd"
"cmd=create-folder name=cert - Test ride server=dev site=Service"
"cmd=create-folder name=cert - Test ride server=dev site=Service permission=locked"
Requirement: I am trying to parse this string and split into a dictionary based on the key and value present on a string .
If user enter First form of Statement, that wold produce a dictionary like :
query_dict = {
'cmd' : 'create-folder',
'name' : 'selfserve - Test ride'
}
if user enter second form of statement that would produce /add the additional key /value pair
query_dict = {
'cmd' : 'create-folder',
'name' : 'selfserve - Test ride',
'server' : 'prd'
}
if user enter third form of statement that would produce
query_dict ={
'cmd' : 'create-folder',
'name' : 'cert - Test ride',
'server' : 'dev',
'site': 'Service'
}
forth form produce the dictionary with key/value split like below
query_dict ={
'cmd' : 'create-folder',
'name' : 'cert - Test ride',
'server' : 'dev',
'site': 'Service',
'permission' : 'locked' }
-idea is to parse a string where key and value are separated with = symbol and where the values can have one or more space and extract the matching key /value pair .
I tried multiple methods to match but unable to figure out a single generic regular expression pattern which can match/extract any string where we have this kind of pattern
Appreciate your help.
i tried several pattern map based different possible user input but that is not a scalable approach . example :
i created three pattern to match three variety of user input but it would be nice if i can have one generic pattern that can match any combination of key=values in a string (i am hard coding the key in the pattern which is not ideal
'(cmd=create-folder).*(name=.*).*' ,
'(cmd=create-pfolder).*(name=.*).*(server=.*).*',
'(cmd=create-pfolder).*(name=.*).*(server=.*).*(site=.*)'