I would recommend using regular expressions for this task, because the language you want to parse is not regular.
You have a character string of several key value pairs. The best way to analyze this is not to match patterns on it, but to label it correctly.
The Python standard library has a module called shlexthat imitates the parsing performed by POSIX shells and provides a lexer implementation that can be easily adapted to your needs.
from shlex import shlex
def parse_kv_pairs(text, item_sep=",", value_sep="="):
"""Parse key-value pairs from a shell-like text."""
lexer = shlex(text, posix=True)
lexer.whitespace = item_sep
lexer.wordchars += value_sep
return dict(word.split(value_sep, maxsplit=1) for word in lexer)
Execution Example:
parse_kv_pairs(
'key1=value1,key2=\'value2,still_value2,not_key1="not_value1"\''
)
Output:
{'key1': 'value1', 'key2': 'value2,still_value2,not_key1="not_value1"'}
EDIT: , , shlex, ( ) , , . , - , (: A="B=\"1,2,3\""), .
(, -, , ), .
EDIT2: split maxsplit, , //. @cdlane !