Regexy
Regexy is the ruby gem that contains a lot of common-use regular expressions (such as email or ip addresses validations) and provides a friendly syntax to combine them.
Table of Contents
Installation
Add this line to your application's Gemfile:
gem 'regexy'
And then execute:
$ bundle
Or install it yourself as:
$ gem install regexy
Usage
Regexy::Regexp
Wrapper around original Regexp class. You can safely use it instead of original one.
r1 = Regexy::Regexp.new('foo')
r2 = Regexy::Regexp.new(/foo/)
r3 = Regexy::Regexp.new(r2)
r4 = Regexy::Regexp.new('foo', Regexp::IGNORECASE)
'abcfoocde' =~ r1
r2.match 'abcfoocde'
Getting the original regexp
For methods, that checks if it's arguments is_a
Regexp instances (for example String#scan
) you can use internal_regexp
method.
str = 'Email me at first@mail.com or second@mail.com'
str.scan(Regexy::Web::Email.new.unbound.internal_regexp).map(&:first)
Combining regular expressions
You can combine your regular expressions with |
operator using |
method (or or
, which is alias for it). Note, that regexp options will be combined too.
Regexy::Regexp.new('foo') | Regexy::Regexp.new(/bar/) # => /foo|bar/
Regexy::Regexp.new(/foo/i) | /bar/x # => /foo|bar/ix
Regexy::Regexp.new(/foo/i).or 'bar'
any_ipv4 = Regexy::Web::IPv4.new(:normal) | Regexy::Web::IPv4.new(:with_port) # matches ip w\ and w\o port
Also you could simply join two expressions using +
method, or it's alias and_then
. Note, that it will remove trailing \z
from first regex and leading \A
from second regex.
Regexy::Regexp.new('foo') + Regexy::Regexp.new(/bar/)
Regexy::Regexp.new(/foo\z/i) + /bar/
Regexy::Regexp.new(/foo/).and_then '\Abar'
Regexy::Regexp.new(/\Afoo\z/).and_then '\Abar\z'
Bound and unbound regular expressions
All build-in regular expressions provided in a form of \A...\z
, which means that they match entire string only. You can remove or add string boundaries using bound
and unbound
methods.
Optional argument method
available (:both
by default) - :left
for manipulating only leading \A
and :right
for trailing \z
.
Regexy::Regexp.new('/Afoo/z').unbound(:left)
Regexy::Regexp.new(/foo/i).bound
str = '0.0.0.0 and 255.255.255.255 are both valid ip addresses'
str.scan(Regexy::Web::IPv4.new.unbound.internal_regexp).flatten
Regexy::Web::Email
Generates regular expressions for email addresses validation (with unicode support). Available options: :relaxed
for general sanity check, :normal
(which is default) with some additional length and ip addresses validations and :strict
for the paranoids.
r1 = Regexy::Web::Email.new(:relaxed)
r2 = Regexy::Web::Email.new(:normal)
r2 = Regexy::Web::Email.new(:strict)
Regexy::Web::Hashtag
Generates regular expressions for matching Hashtags.
A hashtag can contain any UTF-8 alphanumeric character, plus the underscore symbol.
A hashtag can't be only numeric, it must have at least one alpahanumeric character or the underscore symbol.
r1 = Regexy::Web::Hashtag.new
Regexy::Web::IPv4
Generates regular expressions for matching IPv4 addresses. Available options: :normal
(by default) for matching ip without port and :with_port
for guess what.
r1 = Regexy::Web::IPv4.new
r1 = Regexy::Web::IPv4.new(:with_port)
any_ipv4 = Regexy::Web::IPv4.new(:normal) | Regexy::Web::IPv4.new(:with_port) # matches ip w\ and w\o port
Regexy::Web::IPv6
Generates regular expressions for matching IPv6 addresses (standard, mixed, and compressed notation are supported). Works in :normal
(by default) and :with_port
modes.
r1 = Regexy::Web::IPv6.new
r1 = Regexy::Web::IPv6.new(:with_port)
any_ipv6 = Regexy::Web::IPv6.new(:normal) | Regexy::Web::IPv6.new(:with_port) # matches ip w\ and w\o port
Regexy::Web::Url
Generates regular expressions for matching Url addresses (with unicode support).
r1 = Regexy::Web::Url.new
Regexy::Web::HostName
Generates regular expressions for matching hostname (with unicode support).
r1 = Regexy::Web::HostName.new
Regexy::Text::Smile
Generates regular expressions for matching smiles.
r = Regexy::Text::Smile.new
str = "Check out http://foo.com :). It's awesome :D"
str.scan(r.unbound.internal_regexp).map(&:first)
Regexy::Text::Emoji
Generates regular expressions for matching emojis.
r = Regexy::Text::Emoji.new
str = "Check out http://foo.com 😀. It's awesome 😼"
str.gsub(r.internal_regexp, 'x_x')
Contributing
Have an idea of new regular expression? Create an issue (some test cases will be much appreciated) or open a pull request.