```ruby
class Language

  include DataMapper::Resource

  # properties

  property :id,   Serial

  property :code, String, :required => true, :unique => true
  property :name, String, :required => true

  # locale string like 'en-US'
  validates_format_of :code, :with => /^[a-z]{2}-[A-Z]{2}$/


  def self.[](code)
    return nil if code.nil?
    first :code => code.to_s.gsub('_', '-')
  end

end
```

Use \A and \z in regexp for stricter matching
user
```ruby
class Language

  include DataMapper::Resource

  # properties

  property :id,   Serial

  property :code, String, :required => true, :unique => true
  property :name, String, :required => true

  # locale string like 'en-US'
  validates_format_of :code, :with => /\A[a-z]{2}-[A-Z]{2}\z/


  def self.[](code)
    return nil if code.nil?
    first :code => code.to_s.gsub('_', '-')
  end

end
```
assistant