# Regex with multiple returns

**URL:** <https://www.ruby-forum.com/t/regex-with-multiple-returns/54203>\
**Category:** Ruby\
**Created:** [December 21, 2005, 6:17am UTC](https://www.ruby-forum.com/t/regex-with-multiple-returns/54203 "2005-12-21T06:17:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 21, 2005, 6:17am UTC](https://www.ruby-forum.com/t/regex-with-multiple-returns/54203/1 "2005-12-21T06:17:13Z")

</div>

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?)

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 21, 2005, 2:00pm UTC](https://www.ruby-forum.com/t/regex-with-multiple-returns/54203/2 "2005-12-21T14:00:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 21, 2005, 11:15am UTC](https://www.ruby-forum.com/t/regex-with-multiple-returns/54203/3 "2005-12-21T11:15:00Z")

</div>

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
```
