Regexifi
A collection of tested common REGEX expressions. Because why re-invent the wheel every time for a new project?
Don't add a bunch of ad-hoc REGEX expressions for common things like email addresses, urls, usernames, time formats - Regexifi instead!
It's not that difficult to add a couple of Regex constant at the top of your class, but it's much more difficult to come up with good, tested and consistent Regex expressions for most common validations.
P.S.:
Please, add more examples and tests, and, of course, more REGEXP! Open an issue if you want to discuss a certain expression or if you don't think it's warranted.
Installation
Add this line to your application's Gemfile:
gem 'regexifi'
And then execute:
$ bundle
Or install it yourself as:
$ gem install regexifi
Usage
You can require the whole gem with all REGEXP, or just specific ones
require 'regexifi'
require 'regexifi/email'
class UrlChecker
include Regexifi
end
UrlChecker::Url::DOMAIN
"my.email@example.com".match? Regexifi::Email::RFC6068
"my.email@example.com".match Regexifi::Email::RFC6068
"my.email@example.com" =~ Regexifi::Email::RFC6068
List of REGEXP in this library
Emails
Suggestion:
If you want to be absolutely true to the spec, most EMAIL RFC are almost impossible to define using Regex alone.
Or it becomes clunky and unwieldy. Here are the most common REGEXP , that are commonly used in the "wild".
If you want to dive a bit more and see why using Regex is not always the best solution - check out this blog post
The top two expressions will get you what you want 99% of the time
Regexifi::Email::DEVISE
Use this if you want to allow internationalized domain names, that use non-Latin alphabet.
Regexifi::Email::IDN
These are also used and are closer to the specs, but they do allow some emails that are for most intents and purposes "spam-y", like john.doe@127.0.0.1
Regexifi::Email::WHATWG
Regexifi::Email::RFC5322
Regexifi::Email::RFC6068
Regexifi::Email::RAILS_TUTORIAL
Refer to code and tests for more details.
Urls
Suggestion: In most cases you can use either URI.parse from Ruby's standard library or an excellent Bob Aman's Addressable gem https://github.com/sporkmonger/addressable, to much better effect
Match a domain without http(s) protocol, i.e: https://www.example.com
Regexifi::Url::DOMAIN
Match the whole url with protocol, i.e: https://example.io/article/2/comments?from=registered_users&recent=true
Regexifi::Url::URL
Refer to code and tests for more details.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
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/[USERNAME]/regexifi-rb.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Regexifi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.