I am trying to create a regex that would find one or more indices of the following pattern:
some text + {text within braces} + {text within braces}
The trick is that the text within braces may include braces as well:
some text + {text withi{n} braces} + {tex{t} within {b}races}
I am able to identify all the three patterns seperately but cannot combine the whole thing so that it would identify the nested inner braces.
import re
import regex
v1_value="A"
v2_value="B"
v_string=rf'\\to{v1_value}or{v2_value}' # dynamically defining the value of the version string
print(f'v_string: {v_string}') # \\toAorB:
match_outer_braces=r"\{(?:[^{}]*|(?R))*\}" # source: https://stackoverflow.com/a/63266732/7147695
whole_pattern=v_string+match_outer_braces*2 # combining the pattern (probably goes wrong here)
sentence1=r"Lorem \toAorB{versionA}{VersionB} ipsum" # sentence with no nested braces
sentence2=r"Lorem \toAorB{versionA}{Ver{s}ionB} ipsum" # sentence with braces within braces
extracted1=regex.findall(whole_pattern,sentence1) # extracts the pattern as desired (no nested braces)
extracted2=regex.findall(match_outer_braces,sentence2) # extracts the outer braces
extracted3=regex.findall(whole_pattern,sentence2) # does not manage to extract the whole pattern with nested braces
print(extracted1) # ['\\toAorB{versionA}{VersionB}']
print(extracted2) # ['{versionA}', '{Ver{s}ionB}']
print(extracted3) # []