Python regex - Replace bracketed text with contents of brackets
问题 I'm trying to write a Python function that replaces instances of text surrounded with curly braces with the contents of the braces, while leaving empty brace-pairs alone. For example: foo {} bar {baz} would become foo {} bar baz . The pattern that I've created to match this is {[^{}]+} , i.e. some text that doesn't contain curly braces (to prevent overlapping matches) surrounded by a set of curly braces. The obvious solution is to use re.sub with my pattern, and I've found that I can