Regex with multiple returns

L

List Recv

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

Robert Klemme

List said:
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
 
W

William James

Robert said:
'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,651
Latest member
VeraPiw932

Latest Threads

Top