0

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

Community
  • 1
  • 1
AJP
  • 21,889
  • 17
  • 76
  • 108
  • have you tried matching the braces "greedily"? – Kash Aug 16 '12 at 14:05
  • @Kash yes, but I wasn't sure how to pair the braces? Also please bear in mind there may be more opening and closing sets proceeding the pair desired. – AJP Aug 16 '12 at 21:27
  • @acheong87 thanks the links to the other questions, I couldn't seem to them before. – AJP Aug 16 '12 at 21:29

0 Answers0