Regular expression to extract text between square or curly braces

As for my previous question , I have a line in the following format:

this {is} a [sample] string with [some] {special} words. [another one] 

What is a regular expression for extracting words in square brackets or , i.e.

 {is} [sample] [some] {special} [another one] 

Note. In my case, using brackets cannot be nested. I would also like to keep enclosing characters so that I can distinguish them when processing the results.

+5
regex
source share
2 answers

Simple or ( | ) different things you want to combine:

\[.*?\]|\{.*?\}

+7
source share

This seems to work:

 [[{].*?[}\]] 

Or this one:

 \[.*?\]|{.*?} 

If you want to catch the cases mentioned in the comments below.

You can use the online regexp tester to try this. I think http://gskinner.com/RegExr/ is one of the most user friendly options.

+2
source share

All Articles