From: "Jesús Gabriel y Galán" Date: 2007-10-08T21:25:27+09:00 Subject: Re: regexp question - look for parentheses then remove them On 10/8/07, Max Williams wrote: > I'm struggling with a regular expression problem, can anyone help? > > I want to take a string, look for anything in parentheses, and if i find > anything, put it into an array, minus the parentheses. > > currently i'm doing this: > > parentheses = /\(.*\)/ > array = string.scan(parentheses) > > This gives me eg > > "3 * (1 + 2)" => ["(1 + 2)"] > > - but is there an easy way to strip the parentheses off before putting > it into the array? > > eg > "3 * (1 + 2)" => ["1 + 2"] > > In addition, if i have nested parentheses inside the outer parentheses, > i want to keep them, eg > > "3 * (1 + (4 / 2))" => ["1 + (4 / 2)"] > > can anyone show me how to do this? x = "3 * (1 + 2)".match(/\((.*)\)/) x.captures => ["1 + 2"] x = "3 * (2 + (1 + 3))".match(/\((.*)\)/) x.captures => ["2 + (1 + 3)"] Hope this helps, Jesus.