How to remove content around and including curly brackets with regex?

Question:

Feel this is a common use case for but i can’t find too much help online.

Given a test string like "Hello my name is name{John} name{Doe}", i would like to remove all curly brackets and the content around them (white space and period is excluding), but keep the content between the brackets. The expected output would be "Hello my name is John Doe".

Asked By: Austin Benny

||

Answers:

Try this:

[^ .]*{|}[^ .]*

[^ .]*{ —> [^ .]* zero or more character except spaces and dots, note there is a space after ^, followed by a literal {

| OR

}[^ .]* —> a literal }, followed by [^ .]* zero or more character except spaces and dots.

See regex demo

Answered By: SaSkY

This should work:

re.sub('[^ .]*{(.*?)}[^ .]*',r'1',string)

Answered By: Jeff
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.