Remove newline characters that appear between curly brackets
Question: I'm currently writing a text processing script which contains static text and
variable values (surrounded by curly brackets). I need to be able to strip out
newline characters but only if they appear between the curly brackets:

`Some text\nwith a {variable\n} value"`

to:

`Some text\nwith a {variable} value"`

Further down in the processing I'm already doing this:

`re.sub(r'\{.*?\}', '(.*)', text, flags=re.MULTILINE|re.DOTALL)`

But I'm not sure how to target just the newline character and not the entirely
of the curly bracket pair. There is also the possibility of multiple newlines:

`Some text\nwith a {variable\n\n\n} value"`

* * *

Using Python 3.x


Answer: You may pass the match object to a lambda in a `re.sub` and replace all
newlines inside `{...}`:

    
    
    import re
    text = 'Some text\nwith a {variable\n} value"'
    print(re.sub(r'{.*?}', lambda m: m.group().replace("\n", ""), text, flags=re.DOTALL))
    

See [online Python 3 demo](http://ideone.com/ElV2ph)

Note that you do not need `re.MULTILINE` flag with this regex as it has no
`^`/`$` anchors to redefine the behavior of, and you do not need to escape `{`
and `}` in the current expression (without excessive backslashes, regexps look
cleaner).

