/espressione

Espressione is a Ruby community-driven common regular expression patterns gem

Primary LanguageRuby

Espressione

Gem Version Build Status Maintainability Test Coverage

Espressione is a ruby gem to provide useful regex patterns.

Installation

Add this line to your application's Gemfile:

gem 'espressione'

And then execute:

$ bundle

Or install it yourself as:

$ gem install espressione

Usage

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)

datetime

  Espressione.datetime.match("2019-04-30 18:37:07 UTC")

date

  Espressione.date.match("2019-04-30")

time

  Espressione.time.match("10:01:12")

uuid

  Espressione.uuid.match("8539ad20-317e-0137-4c89-7aa46e47cfb4")

ip

  Espressione.ip.match("127.0.0.1")

ipv6

  Espressione.ipv6.match("2001:0db8:85a3:0000:0000:8a2e:0370:7334")

url

  Espressione.url.match("https://github.com/dvinciguerra")

email

  Espressione.email.match("joe@doe.com")

subdomain

  Espressione.subdomain.match("shipit.resultadosdigitais.com.br")

html_tag

  Espressione.html_tag.match("<body>")

Development

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dvinciguerra/espressione.