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

J

Jedrin

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"]]
 
S

sreservoir

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"]]

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.
 

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
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top