Regex with multiple returns

Discussion in 'Ruby' started by List Recv, Dec 21, 2005.

  1. List Recv

    List Recv Guest

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

    --
    Posted via http://www.ruby-forum.com/.
     
    List Recv, Dec 21, 2005
    #1
    1. Advertising

  2. 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
     
    Robert Klemme, Dec 21, 2005
    #2
    1. Advertising

  3. 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
     
    William James, Dec 21, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vizlab
    Replies:
    3
    Views:
    4,208
    Michael Bar-Sinai
    Oct 17, 2007
  2. Replies:
    3
    Views:
    768
    Reedick, Andrew
    Jul 1, 2008
  3. ++imanshu
    Replies:
    7
    Views:
    476
    ++imanshu
    Aug 23, 2008
  4. Trans
    Replies:
    2
    Views:
    226
    Trans
    Nov 6, 2005
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    623
    David A. Black
    Jul 2, 2008
Loading...

Share This Page