[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Regex with multiple returns

Robert Klemme

12/21/2005 10:08:00 AM

List Recv wrote:
> Is there a Ruby regex that can return all of the following:
>
boringstuff(returnthisplease)notthis(butthis)dontwantthis(butIdowantthisno
matterhowmanyparenthiesisthereare)
>
> If I only had one pair of parens, I would just do:
> /[^\(\)]+\((\w+?)\)/
>
> but for varying amounts I don't know how to do it.
>
> (Maybe I could group the whole thing in parens and put a + after it -
> but then it would return the whole thing also - is there a way around
> this?)

>>
'boringstuff(returnthisplease)notthis(butthis)dontwantthis(butIdowantthisn
omatterhowmanyparenthiesisthereare)'.scan(/\(([^)]*)\)/).map {|m|m[0]}
=> ["returnthisplease", "butthis",
"butIdowantthisnomatterhowmanyparenthiesisthereare"]

robert

1 Answer

William James

12/21/2005 12:56:00 PM

0

Robert Klemme wrote:

> 'boringstuff(returnthisplease)notthis(butthis)dontwantthis(butIdowantthisn
> omatterhowmanyparenthiesisthereare)'.scan(/\(([^)]*)\)/).map {|m|m[0]}
> => ["returnthisplease", "butthis",
> "butIdowantthisnomatterhowmanyparenthiesisthereare"]
>
> robert

p "junk(keep)trash(this)detritus(text)".scan(/\((.*?)\)/).flatten