I have some input parameters like:
param1=param1Value¶m2=param2Value¶m3=param3Value…
These parameters are linked with the character ‘&‘, the parameter and its value is linked with ‘=‘. I want to parse them into a key-value-map like this:
[param1, param1Value], [param2, param2Value], [param3, param3Value],...
But because the parameter’s value contains the tow key-characters, this would break the parameter-parsing procedure.
I was informed the parameter passer, who created these parameter format to easy customize, such as changing the parameter value for different application using sprintf. So its hard to persuade them to separator these parameters in a map-array.
I was originally going to use yacc/lex, but is to complicated to such a little-feature. sscanf can also parse text something like regexp, but it does not seem so easy to build a regexp-like string for my parameter-list.
Is there any simple but reliable way to parse them without key-character?
You can use
strtokto tokenize the string at the'&'character, then split the “tokens” at'='to get the parameter names and values.The splitting at
'='can either be done withstrtokas well (or ratherstrtok_r) or usingstrchrandstrncpy/strcpyorstrndup/strdup.