re = /^\d(?=(?:-?\d){7}-?\d-\d$)\d*-\d+-\d*\d-\d$/m
str = '01-234-5678-9
01234-56-78-9
0123-4-5678-9
012-345-678-9
01-234567-8-9
01-234-5678-9
0-12345-678-9
0-123-45678-9
0-123-45678-9
01-23456-78-9
0-123456-78-9
0-1234567-8-9
No Match:
01-234-56788-9
01-234-568-9
01--2345678-9
01-2345678--9
0-1-23456789
-01-2345678-9
0-1238787456-78-9
0-123456-78-3439
0-123456-738-9
30-123456-78-9'
# 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