regexp multiple matches

Discussion in 'Ruby' started by Mickael Faivre-Macon, Jan 28, 2009.

  1. Hi,

    /(\d+),(\d+)(;(\d+),(\d+))*/

    This regexp matches are:
    0: (5,1;5,7;3,2)
    1: (5)
    2: (1)
    3: (;3,2)
    4: (3)
    5: (2)

    My question is why ;5,7 is not matched ?

    Thanks,
    Mickael.
    --
    Posted via http://www.ruby-forum.com/.
    Mickael Faivre-Macon, Jan 28, 2009
    #1
    1. Advertising

  2. Hi --

    On Wed, 28 Jan 2009, Mickael Faivre-Macon wrote:

    > Hi,
    >
    > /(\d+),(\d+)(;(\d+),(\d+))*/
    >
    > This regexp matches are:
    > 0: (5,1;5,7;3,2)
    > 1: (5)
    > 2: (1)
    > 3: (;3,2)
    > 4: (3)
    > 5: (2)
    >
    > My question is why ;5,7 is not matched ?


    What's the string?


    David

    --
    David A. Black / Ruby Power and Light, LLC
    Ruby/Rails consulting & training: http://www.rubypal.com
    Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

    http://www.wishsight.com => Independent, social wishlist management!
    David A. Black, Jan 28, 2009
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon <>wrote:

    > Hi,
    >
    > /(\d+),(\d+)(;(\d+),(\d+))*/
    >
    > This regexp matches are:
    > 0: (5,1;5,7;3,2)
    > 1: (5)
    > 2: (1)
    > 3: (;3,2)
    > 4: (3)
    > 5: (2)
    >
    > My question is why ;5,7 is not matched ?
    >
    > Thanks,
    > Mickael.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Mickael

    Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2 which
    matches based on the '*'

    --
    Andrew Timberlake
    http://ramblingsonrails.com
    http://www.linkedin.com/in/andrewtimberlake

    "I have never let my schooling interfere with my education" - Mark Twain
    Andrew Timberlake, Jan 28, 2009
    #3
  4. And is there a way to keep previous matching ?
    Mickael.


    Andrew Timberlake wrote:
    > On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon
    > <>wrote:
    >
    >> 5: (2)
    >>
    >> My question is why ;5,7 is not matched ?
    >>
    >> Thanks,
    >> Mickael.
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>
    >>

    > Mickael
    >
    > Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2
    > which
    > matches based on the '*'
    >
    > --
    > Andrew Timberlake
    > http://ramblingsonrails.com
    > http://www.linkedin.com/in/andrewtimberlake
    >
    > "I have never let my schooling interfere with my education" - Mark Twain


    --
    Posted via http://www.ruby-forum.com/.
    Mickael Faivre-Macon, Jan 28, 2009
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    On Wed, Jan 28, 2009 at 5:00 PM, Mickael Faivre-Macon <>wrote:

    > And is there a way to keep previous matching ?
    > Mickael.
    >
    >
    > Andrew Timberlake wrote:
    > > On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon
    > > <>wrote:
    > >
    > >> 5: (2)
    > >>
    > >> My question is why ;5,7 is not matched ?
    > >>
    > >> Thanks,
    > >> Mickael.
    > >> --
    > >> Posted via http://www.ruby-forum.com/.
    > >>
    > >>

    > > Mickael
    > >
    > > Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2
    > > which
    > > matches based on the '*'
    > >
    > > --
    > > Andrew Timberlake
    > > http://ramblingsonrails.com
    > > http://www.linkedin.com/in/andrewtimberlake
    > >
    > > "I have never let my schooling interfere with my education" - Mark Twain

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

    What about tackling it from a completely different angle

    s = "5,1;5,7;3,2"
    pairs = s.split(';') #=> ["5,1", "5,7", "3,2"]
    pairs.each do |pair|
    pair.split(',') #=> ["5", "1"] etc
    end

    --
    Andrew Timberlake
    http://ramblingsonrails.com
    http://www.linkedin.com/in/andrewtimberlake

    "I have never let my schooling interfere with my education" - Mark Twain
    Andrew Timberlake, Jan 28, 2009
    #5
  6. Sure, that's what I've just done :)
    Just wondering.
    Thanks anyway.

    Mickael.

    Andrew Timberlake wrote:
    > On Wed, Jan 28, 2009 at 5:00 PM, Mickael Faivre-Macon
    > <>wrote:
    >
    >> >> My question is why ;5,7 is not matched ?
    >> > which

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

    > What about tackling it from a completely different angle
    >
    > s = "5,1;5,7;3,2"
    > pairs = s.split(';') #=> ["5,1", "5,7", "3,2"]
    > pairs.each do |pair|
    > pair.split(',') #=> ["5", "1"] etc
    > end
    >
    > --
    > Andrew Timberlake
    > http://ramblingsonrails.com
    > http://www.linkedin.com/in/andrewtimberlake
    >
    > "I have never let my schooling interfere with my education" - Mark Twain


    --
    Posted via http://www.ruby-forum.com/.
    Mickael Faivre-Macon, Jan 28, 2009
    #6
  7. On Jan 28, 2009, at 10:00 AM, Mickael Faivre-Macon wrote:

    > Andrew Timberlake wrote:
    >> On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon
    >> <>wrote:
    >>
    >>> 5: (2)
    >>>
    >>> My question is why ;5,7 is not matched ?
    >>>
    >>> Thanks,
    >>> Mickael.
    >>> --
    >>> Posted via http://www.ruby-forum.com/.
    >>>
    >>>

    >> Mickael
    >>
    >> Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;
    >> 3,2
    >> which
    >> matches based on the '*'
    >>
    >>
    >>
    >> What about tackling it from a completely different angle
    >>
    >> s = "5,1;5,7;3,2"
    >> pairs = s.split(';') #=> ["5,1", "5,7", "3,2"]
    >> pairs.each do |pair|
    >> pair.split(',') #=> ["5", "1"] etc
    >> end
    >>
    >> --
    >> Andrew Timberlake
    >> http://ramblingsonrails.com
    >> http://www.linkedin.com/in/andrewtimberlake
    >>
    >> "I have never let my schooling interfere with my education" - Mark
    >> Twain
    >>

    >
    > And is there a way to keep previous matching ?
    > Mickael.


    >
    > --


    irb> re = /(\d+),(\d+)(;(\d+),(\d+))*/
    => /(\d+),(\d+)(;(\d+),(\d+))*/
    irb> s = "5,1;5,7;3,2"
    => "5,1;5,7;3,2"
    irb> s.match(re).captures
    => ["5", "1", ";3,2", "3", "2"]

    You can wrap another set of parentheses in there:

    irb> re = /(\d+),(\d+)((;(\d+),(\d+))*)/
    => /(\d+),(\d+)((;(\d+),(\d+))*)/
    irb> s.match(re).captures
    => ["5", "1", ";5,7;3,2", ";3,2", "3", "2"]

    But in addition to Andrew's split, you could use String#scan

    irb> s.scan(/(\d+),(\d+)/)
    => [["5", "1"], ["5", "7"], ["3", "2"]]

    It all depends on what you're trying to accomplish.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Jan 28, 2009
    #7
    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. Boris Pelakh
    Replies:
    3
    Views:
    461
    Purl Gurl
    Apr 8, 2004
  2. Replies:
    3
    Views:
    1,557
  3. gry
    Replies:
    4
    Views:
    222
  4. Kevin Howe

    multiple regexp matches

    Kevin Howe, Aug 17, 2004, in forum: Ruby
    Replies:
    27
    Views:
    274
    Martin DeMello
    Aug 24, 2004
  5. Joao Silva
    Replies:
    16
    Views:
    353
    7stud --
    Aug 21, 2009
Loading...

Share This Page