re = /^(([A-Za-z\d]+[\-_])*[A-Za-z\d]{2,}\.)*(?:testnet|near)$/m
str = '// VALID
example.near
example.testnet
sub.xtrtarstsrt3322.testnet
1sub.sub.near
12.near
aa.testnet
1a.a3a.near
deltron-me.testnet
deltron_yes.near
// INVALID because of the capital letter but we can lowercase it on db
Fmish.testnet
// INVALID
aa.setetstestnet
_sentaent.testnet
-howabout.this.near
space should fail.testnet
space should fail.near
!testnet.testnet
2a!.near
n.testnet
shouldnotendwithperiod.testnet.
aa.t.testnet
1a..near
touchingDotsShouldfail..testnet
.aa.testnet'
# 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