Reputation: 1
I was working on a coderbyte problem where I output the number of occurrences of a character along with the corresponding character. For example "wwwggopp" would return 3w2g1o2p. I was able to solve it but I compared my answer to someone else's and they came up with the following:
def RunLength(str)
chunks = str.scan(/((\w)\2*)/)
output = ' '
chunks.each do |chunk|
output << chunk[0].size.to_s + chunk[1]
end
output
end
I get most of the code but what exactly is happening here?
(/((\w)\2*)/)
I understand that \w refers to any character and \2 is a 'backreference' and * refers to 0 or more instances...but together, I'm not sure what it means, mostly because I don't know really know what a backreference is and how it works. I've been reading about it but I'm still struggling to grasp the concept. Does the \2 refer to the "2nd group" and if so, what exactly is the "2nd group"?
Upvotes: 0
Views: 251
Reputation: 70722
Backreferences recall what was matched by a capturing group. A backreference is specified as a backslash (\
); followed by a digit indicating the number of the group to be recalled.
Your regular expression broke down:
( # group and capture to \1:
( # group and capture to \2:
\w # word characters (a-z, A-Z, 0-9, _)
) # end of \2
\2* # what was matched by capture \2 (0 or more times)
) # end of \1
Upvotes: 3