Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading