Forum: Ruby Regex with multiple returns

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
List R. (Guest)
on 2005-12-21 07:17
Is there a Ruby regex that can return all of the following:
boringstuff(returnthisplease)notthis(butthis)dontwantthis(butIdowantthisnomatterhowmanyparenthiesisthereare)

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?)
Robert K. (Guest)
on 2005-12-21 12:15
(Received via mailing list)
List R. 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
William J. (Guest)
on 2005-12-21 15:00
(Received via mailing list)
Robert K. 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
This topic is locked and can not be replied to.