[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regexp question - look for parentheses then remove them

Max Williams

10/8/2007 12:02:00 PM

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?

thanks
max
--
Posted via http://www.ruby-....

5 Answers

Jesús Gabriel y Galán

10/8/2007 12:25:00 PM

0

On 10/8/07, Max Williams <toastkid.williams@gmail.com> 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.

Max Williams

10/8/2007 12:39:00 PM

0

Jesús Gabriel y Galán wrote:
> On 10/8/07, Max Williams <toastkid.williams@gmail.com> wrote:
>> This gives me eg
>> 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.

ah, "captures" - that's the same as MatchData#to_a, right? Perfect,
thanks!
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

10/8/2007 12:59:00 PM

0

On 10/8/07, Max Williams <toastkid.williams@gmail.com> wrote:
> Jesús Gabriel y Galán wrote:
> > On 10/8/07, Max Williams <toastkid.williams@gmail.com> wrote:
> >> This gives me eg
> >> 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.
>
> ah, "captures" - that's the same as MatchData#to_a, right? Perfect,

Not exactly, because the MatchData#to_a returns as the first position
of the array the string that matched, and then starting from x[1] the
captured groups. MatchData#captures only contains the captures. See
the difference:

irb(main):001:0> a = "123456".match(/(.)(.)\d\d/)
=> #<MatchData:0xb7c97a04>
irb(main):002:0> a.to_a
=> ["1234", "1", "2"]
irb(main):003:0> a.captures
=> ["1", "2"]

Jesus.

ThoML

10/9/2007 7:28:00 AM

0

> ah, "captures"

You can access the match data right away:

x = /\((.*)\)/.match("3 * (1 + 2)")
x[1]
or $1

I'd also make the * non-greedy -> *?

/\((.*?)\)/.match("3 * (1 + 2) * (3 + 4)")[1]
=> "1 + 2"

but:
/\((.*)\)/.match("3 * (1 + 2) * (3 + 4)")[1]
=> "1 + 2) * (3 + 4"

Max Williams

10/9/2007 2:30:00 PM

0

tho_mica_l wrote:
> I'd also make the * non-greedy -> *?
>
> /\((.*?)\)/.match("3 * (1 + 2) * (3 + 4)")[1]
> => "1 + 2"
>
> but:
> /\((.*)\)/.match("3 * (1 + 2) * (3 + 4)")[1]
> => "1 + 2) * (3 + 4"

Excellent tip, cheers!
--
Posted via http://www.ruby-....