NoRegex
Write Ruby without regex! A gem that provides simple, readable methods to replace complex regular expressions for string validation and manipulation.
Transform Your Code
BEFORE - From complex regex patterns:
validates :email, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }
validates :phone, format: { with: /\A\+?[\d\s\-\(\)]+\z/ }
if params[:search].match?(/\A\d+\z/)
end
user_input.gsub(/[`a-zA-Z0-9]/, '')
AFTER - To simple, readable methods!
validates :email, format: { with: is_email? }
validates :phone, format: { with: is_phone_number? }
if params[:search].is_number?
end
user_input.remove_special_chars
No more googling regex patterns or debugging cryptic expressions!
Installation
Add this line to your application's Gemfile:
gem 'no_regex'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install no_regex
Rails Integration
To use the format: { with: is_number? } syntax in your Rails models or form objects, you need to extend your class with NoRegex::PatternMethods:
class User < ApplicationRecord
extend NoRegex::PatternMethods
validates :email, format: { with: is_email? }
validates :phone, format: { with: is_phone_number? }
end
For form objects:
class MyFormObject
include ActiveModel::Model
extend NoRegex::PatternMethods
validates :search_term, format: { with: is_number? }
end
This makes all the pattern methods available to your validations. Once extended, you can use any of the validation methods in the Format Validations section.
Usage
NoRegex extends Ruby's String, Integer, Float, and other classes with intuitive methods that eliminate the need for regex:
String Methods
require 'no_regex'
"12345".is_number?
"test@example.com".is_email?
"https://example.com".is_url?
"john_doe".is_username?
"hello-world".remove_dashes
"abc123xyz".keep_numbers
"HelloWorld".to_snake_case
"Contact: test@example.com".extract_emails
"Price: $123".extract_numbers
Numeric Methods
42.is_positive?
123.to_currency
3.14.to_percentage
Validation Methods
is_number? - Check if string contains a number
is_letters? - Check if string contains only letters
is_integer? - Check if string is an integer
is_decimal? - Check if string is a decimal number
is_alphanumeric? - Check if string contains only letters and numbers
is_email? - Check if string is a valid email format
is_blank? - Check if string is nil or contains only whitespace
is_url? - Check if string is a valid URL format
is_phone_number? - Check if string is a valid phone number
is_zip_code? - Check if string is a valid ZIP code
is_hex_color? - Check if string is a valid hex color
is_username? - Check if string is a valid username (letters, numbers, _, -)
is_positive_number? - Check if string is a positive number
is_uuid? - Check if string is a valid UUID
is_credit_card? - Check if string is a valid credit card number
is_ssn? - Check if string is a valid SSN
is_ipv4? - Check if string is a valid IPv4 address
is_time_24h? - Check if string is valid 24-hour time format
is_date_yyyy_mm_dd? - Check if string is valid YYYY-MM-DD date format
Manipulation Methods
remove_dashes - Remove all dashes from string
remove_spaces - Remove all spaces from string
remove_special_chars - Remove all special characters, keeping only letters and numbers
remove_numbers - Remove all numbers from string
remove_letters - Remove all letters from string
keep_numbers - Keep only numbers in string
keep_letters - Keep only letters in string
Conversion Methods
to_snake_case - Convert string to snake_case
to_camel_case - Convert string to camelCase
extract_numbers - Extract all numbers from string (returns array)
extract_emails - Extract all email addresses from string (returns array)
Utility Methods
word_count - Count words in string
truncate(length, ellipsis) - Truncate string to specified length with ellipsis
Format Validators
validates :field, format: { with: is_number? }
validates :field, format: { with: is_letters? }
validates :field, format: { with: is_integer? }
validates :field, format: { with: is_decimal? }
validates :field, format: { with: is_alphanumeric? }
validates :field, format: { with: is_email? }
validates :field, format: { with: is_phone_number? }
validates :field, format: { with: is_url? }
validates :field, format: { with: is_zip_code? }
validates :field, format: { with: is_hex_color? }
validates :field, format: { with: is_username? }
validates :field, format: { with: is_positive_number? }
validates :field, format: { with: is_uuid? }
validates :field, format: { with: is_credit_card? }
validates :field, format: { with: is_ssn? }
validates :field, format: { with: is_ipv4? }
validates :field, format: { with: is_time_24h? }
validates :field, format: { with: is_date_yyyy_mm_dd? }
Custom Validators (Alternative Approach)
class User < ApplicationRecord
validates :phone, number: true
validates :name, letters: { message: "can only contain letters" }
validates :username, alphanumeric: true
validates :email, email_format: true
validates :website, url_format: { allow_blank: true }
end
Examples
"12345".is_number?
"abc123".is_letters?
"test@example.com".is_email?
"https://example.com".is_url?
"hello-world-123".remove_dashes
"hello world 123".remove_numbers
"abc123xyz".keep_numbers
"abc123xyz".keep_letters
"Hello@World#123!".remove_special_chars
"HelloWorld".to_snake_case
"hello_world".to_camel_case
"Contact: test@example.com or admin@site.org".extract_emails
"Price is $123 or $456".extract_numbers
"Hello world, this is a test".word_count
"This is a very long string".truncate(15)
42.is_positive?
-10.is_negative?
100.to_currency
1234.to_formatted
0.75.to_percentage
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests.
To install this gem onto your local machine, run bundle exec rake install.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/Derity/no_regex
License
The gem is available as open source under the terms of the MIT License.