in reply to Regular expressions: Extracting certain text from a line

You have \{.+?\}.

From your description, you don't want [] within {...} and you don't want {} within [ ... ], so your "in between" groups should reflect that:

/(\{[^\[\]]{}]+?\})|.../

I think there are ways to better extract stuff within matching pairs of parentheses, but in the long run, you'll have to look at a proper parser for your grammar.

Replies are listed 'Best First'.
Re^2: Regular expressions: Extracting certain text from a line
by Wcool (Novice) on Apr 07, 2014 at 11:45 UTC
    I used the
    tag as the pseudo HTML is creating havoc on square brackets. <code> Basically I want the most outer [ some chars] or { some chars } but no +t [] or {} 2 other examples: 1) a[] = [ this is a test { test2 } ] Should only match [ this is a test { test2 } ] 2) a[] = [ this is a [ test ] { test2 } ] Should return [ this is a [ test ] { test2 } ]

    I simplified by looking only at square brackets but still no joy.

    I thought of something like this:

    \[ <- a square bracket .[^\/] <- followed by any character but not an end bracket + <- at least one character
    I give up, I will just look for brackets and if it matches empty brackets I filter them out in the code

      Don't give up too quickly!

      (\[(?:[^\[\]]++|(?1))+\])

      That should get you started ;-)