Espressione is a ruby gem to provide useful regex patterns.
Add this line to your application's Gemfile:
gem 'espressione'
And then execute:
$ bundle
Or install it yourself as:
$ gem install espressione
You can import your set of expressions directly from the YAML file.
# In phone.yml
local: '\(?[0-9]{2,3}\)? ?[0-9]{3,4}[ -]?[0-9]{4}'
international: '\+[0-9]{2}? \(?[0-9]{2,3}\)? ?[0-9]{3,4}[ -]?[0-9]{4}'
require "espressione"
Espressione.load "phone.yml", prefix: :phone
Espressione.phone_local # => /\(?[0-9]{2,3}\)? ?[0-9]{3,4}[ -]?[0-9]{4}/
Espressione.phone_international # => /\+[0-9]{2}? \(?[0-9]{2,3}\)? ?[0-9]{3,4}[ -]?[0-9]{4}/
Or you can use our standard set of expressions:
datetime, date, time, uuid, ip, ipv6, url, email, subdomain, html_tag
# validate simple email
email = 'joe@doe.com'
puts "Contains a valid e-mail" if Espressione.email.match(email)
# validate url
url = "https://github.com/dvinciguerra"
puts "Contains a valid url" if Espressione.url.match(url)
# validate a uuid
uuid = '8539ad20-317e-0137-4c89-7aa46e47cfb4'
puts "Contains a valid uuid" if Espressione.uuid.match(uuid)
# validate a datetime
datetime = Time.now.utc
puts "Contains a valid datetime" if Espressione.datetime.match(datetime.to_s)
# validate a ip
ip = '192.168.1.1'
puts "Contains a valid ip" if Espressione.ip.match(ip)
Espressione.datetime.match("2019-04-30 18:37:07 UTC")
Espressione.date.match("2019-04-30")
Espressione.time.match("10:01:12")
Espressione.uuid.match("8539ad20-317e-0137-4c89-7aa46e47cfb4")
Espressione.ip.match("127.0.0.1")
Espressione.ipv6.match("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
Espressione.url.match("https://github.com/dvinciguerra")
Espressione.email.match("joe@doe.com")
Espressione.subdomain.match("shipit.resultadosdigitais.com.br")
Espressione.html_tag.match("<body>")
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/dvinciguerra/espressione.