re = /^\d*?(?<first>(?<a>\d)\k<a>)\d*?(?!\k<first>)(?<second>(?<b>\d)\k<b>)\d*$/m
str = 'What should match (where # is just any digit, matching 1 or 2 or not)
11722
11227
71122
What should not match
11711
22227
88888
11x22
1122x
x1122
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html