I cant seem to get regex to work with the following example. Basically I would like to parse 4 groups from a string such as below:
test.this
test[extra].this
test[extra].this{data}
test.this{data}
I would like to get the answer as such, for the examples above respectively:
val1='test', val2=None, val3='this', val4=None
val1='test', val2='extra', val3='this', val4=None
val1='test', val2='extra', val3='this', val4='data'
val1='test', val2=None, val3='this', val4='data'
I tried this but it's not working:
import re
tests = ["test.this",
"test[extra].this",
"test[extra].this{data}",
"test.this{data}",]
for test in tests:
m = re.match(r'^([^\[\.]+)(?:\[([^\]]+)])(?:\.([^{]+){)([^}]+)?$', test)
if m:
print(test, '->', m[1], m[2], m[3], m[4])