Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regex with multiple returns

Reply
Thread Tools

Regex with multiple returns

 
 
List Recv
Guest
Posts: n/a
 
      12-21-2005
Is there a Ruby regex that can return all of the following:
boringstuff(returnthisplease)notthis(butthis)dontw antthis(butIdowantthisnomatterhowmanyparenthiesist hereare)

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

--
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-21-2005
List Recv wrote:
> Is there a Ruby regex that can return all of the following:
>

boringstuff(returnthisplease)notthis(butthis)dontw antthis(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)dont wantthis(butIdowantthisn
omatterhowmanyparenthiesisthereare)'.scan(/\(([^)]*)\)/).map {|m|m[0]}
=> ["returnthisplease", "butthis",
"butIdowantthisnomatterhowmanyparenthiesisthereare "]

robert

 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      12-21-2005
Robert Klemme wrote:

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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorted Returns List and Reversed Returns Iterator ++imanshu Python 7 08-23-2008 04:25 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
block returns and hash element returns Trans Ruby 2 11-06-2005 12:15 PM



Advertisments