Given a dict and a string:
my_dict = {"X":"xxx", "Y":"yyy"}
my_str = "A[xxx]BC[yyy]" # editted to comment below
I need to create two different strings:
> print(result_1)
'ABC'
> print(result_2)
1|X|3|Y
where result_1 is my_str without the square brackets, and result_2 is the index of that place in the string without brackets.
So far, I am able to find all square brackets with:
vals = re.findall(r'\[([^]]*)\]', my_str)
for val in vals:
print(val)
I know that I can find the index with str.index() or str.find() as explained here, and I also know that I can use re.sub() to replace values, but I need to combine these methods with the lookup in the dict to obtain two different strings. Can anyone help me or put me on the right path?