views:

376

answers:

4

hi there,

is there a regular expression to match the content within braces. For example with the following:

d = {'key': {'a': [1,2,3]}}

I would want to match {'key': {'a': [1,2,3]}} and {'a': [1,2,3]}, but not {'key': {'a': [1,2,3]}

+2  A: 

In classical regular expressions, this is impossible - DFAs can't parse nested pairs.

There are ways to do it with extended regular expressions, such as recursive expressions that are allowed in some regex engines (such as Perl regex), but they're not always pretty. (too much php provided the Perl version: /\{(?:[^{}]+|(?R))*\}/ with the (?R) option being the recursive match.)

You don't necessarily need regex to do this kind of thing though. You could do it simply by walking through the list and keeping a stack of open braces (and what position they were seen at). Then whenever you see an open brace, you push its position onto the stack, and whenever you see a close brace, you pop the most recently seen open brace off the stack, and use its position plus the current position as the bounds for a substring which becomes one of your matches. Repeat until you reach the end of the string.

Amber
walking through - yeah that's my current solution. Wasn't sure whether it was possible with regular RE's.
Plumo
see also http://stackoverflow.com/questions/968919/when-not-to-use-regex-in-c-or-java-c-etc
Ian Ringrose
A: 

Regular expressions can't handle nesting, so there is no regular expression that will work in the general case.

If you can limit the maximum nesting depth you can probably construct an expression that checks explicitly for all the possible levels of nesting. Generally you'll probably better off using some kind of parser framework.

sth
There **are** variants that allow arbitrary nesting.
Brad Gilbert
A: 

The PCRE regex library can do this using recursion:

/\{(?:[^{}]+|(?R))*\}/
too much php
+1  A: 

It's fairly simple but it finds a match :)

{'key': {'\w+': \[[\w,]*\w\]}}

Please don't vote me down I'm just trying to help :(

Alex
unfortunately the example I gave was a simplified version of the real data I am dealing with...
Plumo