I don't quite understand tricky reg ex with back ref

Discussion in 'Ruby' started by Jedrin, Feb 18, 2012.

  1. Jedrin

    Jedrin Guest

    I am trying to understand exactly why this reg ex works the way it
    does.
    The key part here is the back ref \2 which confuses me a bit as it
    seems recursive in a way
    that I haven't intuited quite:

    s = "111221"
    s.scan(/((\d)\2*)/).map(&:first)
    #=> ["111", "22", "1"]

    I figured out that that map(&:first)
    is the same as map{|x| x.first}

    so then it's this part that I don't quite understand
    s.scan(/((\d)\2*)/)
    #=>[["111", "1"], ["22", "2"], ["1", "1"]]
     
    Jedrin, Feb 18, 2012
    #1
    1. Advertisements

  2. Jedrin

    sreservoir Guest

    in each subarray, the first is the outer paren-capture, and the second
    is the inner capture.

    the inner set of parens captures a single digit; once that is captured,
    \2 refers to that specific match.

    thus, the regex searches for a single digit, followed by a non-negative
    integer number of repetitions of the same digit; all the repetitions go
    in the first capture; the specific digit in the second.
     
    sreservoir, Feb 18, 2012
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.