If you will continue to use the identical string format, you may use a complex regular expression to cover various patterns.
The following is a regular expression for a few patterns I have anticipated. Let me know if this solves your issue. If not, please provide details on where my solution fails:
Edit for explaining
- Group one
(?=F\d+=\[).*?(\]){2,}(?=|,)
(?=F\d+=\[)
match string begin with F, have one or more numbers, and have =[
after them
.*?(\]){2,}
match string as less as it can, and it have 2 or more ]
at the end
(?=|,)
the string end ,
or nothing
- Group two
F\d.[^,]+
- after matching group one, it is easy to find the string like
F6=Value6
. because the first group takes precedence over the second group, so the second group can only match the remaining strings.
Code
string = "F1=Value1,F2=Value2,F3=[[F4=Value4,F5=Value5]],F6=Value6,F7=[[[BC],[AC]]]"
import re
resultset = [ele[0] if ele[0] else ele[-1] for ele in re.findall(r"((?=F\d+=\[).*?(\]){2,}(?=|,))|(F\d.[^,]+)",string)]
print({x[:x.find("=")]:x[x.find("=")+1:] for x in resultset})
Output
{'F6': 'Value6', 'F2': 'Value2', 'F3': '[[F4=Value4,F5=Value5]]', 'F1': 'Value1', 'F7': '[[[BC],[AC]]]'}
Edit: because the former regex does not work when string is F1=Value1,F2=Value2,F8=[iD=10,ig=12S],F3=[[F4=Value4,F5=Value5]],F6=Value6,F9=[iD=10,ig=12S],F7=[[[BC],[AC]]]
I changed regex to ((?=F\d+=\[).*?(\])+(?= |,F))|(F\d.[^,]+)
. And we need one trick. add one blankspace after string.
string = "F1=Value1,F2=Value2,F8=[iD=10,ig=12S],F3=[[F4=Value4,F5=Value5]],F6=Value6,F9=[iD=10,ig=12S],F7=[[[BC],[AC]]]"
import re
resultset = [ele[0] if ele[0] else ele[-1] for ele in re.findall(r"((?=F\d+=\[).*?(\])+(?= |,F))|(F\d.[^,]+)",string+" ")]
print({x[:x.find("=")]:x[x.find("=")+1:] for x in resultset})