Possible Duplicate:
Regular Expression to match outer brackets
Is there a regex that will return:
[aaaa[aaa]aaaa]
from
aaa"fields":[aaaa[aaa]aaaa]aaa
This is in the context of getting an array from a JSON string that also contains XPaths. The XPaths have opening and closing square braces just like the array, hence the generalised problem of retriving a string delimited by opening and closing braces that it also contains as substrings... I hope that makes sense.
I think you need to keep track of the number of opened pairs of square braces and only when it has reached 0 do you return that portion of the string. This would require a loop but I was wondering if there a pure regexp solution.
My first attempt was:
cleaned_defintion = defintion.gsub(/\[\d*\]/,"")
which transforms XPaths such as this: html[1]/body[1]/form[1]/div[7]/div[2]/input[1]
to html/body/form/div/div/input
but this will leave xpaths with elements like:
td[@width='113']
which then breaks:
json_fields = cleaned_defintion.match(/fields":(\[[^\]]*\])/)
This question has an answer that uses the iterating approach: Best way to find nested opening and closing tags