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/)
# It's a number
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?
# It's a 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'
# Validation methods - return true/false
"12345".is_number? # => true
"[email protected]".is_email? # => true
"https://example.com".is_url? # => true
"john_doe".is_username? # => true
# Manipulation methods - return modified strings
"hello-world".remove_dashes # => "helloworld"
"abc123xyz".keep_numbers # => "123"
"HelloWorld".to_snake_case # => "hello_world"
# Extraction methods - return arrays
"Contact: [email protected]".extract_emails # => ["[email protected]"]
"Price: $123".extract_numbers # => ["123"]
Numeric Methods
42.is_positive? # => true
123.to_currency # => "$123"
3.14.to_percentage # => "314.0%"
Validation Methods
is_number?- Check if string contains a numberis_letters?- Check if string contains only lettersis_integer?- Check if string is an integeris_decimal?- Check if string is a decimal numberis_alphanumeric?- Check if string contains only letters and numbersis_email?- Check if string is a valid email formatis_blank?- Check if string is nil or contains only whitespaceis_url?- Check if string is a valid URL formatis_phone_number?- Check if string is a valid phone numberis_zip_code?- Check if string is a valid ZIP codeis_hex_color?- Check if string is a valid hex coloris_username?- Check if string is a valid username (letters, numbers, _, -)is_positive_number?- Check if string is a positive numberis_uuid?- Check if string is a valid UUIDis_credit_card?- Check if string is a valid credit card numberis_ssn?- Check if string is a valid SSNis_ipv4?- Check if string is a valid IPv4 addressis_time_24h?- Check if string is valid 24-hour time formatis_date_yyyy_mm_dd?- Check if string is valid YYYY-MM-DD date format
Manipulation Methods
remove_dashes- Remove all dashes from stringremove_spaces- Remove all spaces from stringremove_special_chars- Remove all special characters, keeping only letters and numbersremove_numbers- Remove all numbers from stringremove_letters- Remove all letters from stringkeep_numbers- Keep only numbers in stringkeep_letters- Keep only letters in string
Conversion Methods
to_snake_case- Convert string to snake_caseto_camel_case- Convert string to camelCase
Extraction Methods
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 stringtruncate(length, ellipsis)- Truncate string to specified length with ellipsis
Format Validators
validates :field, format: { with: is_number? } # Only digits
validates :field, format: { with: is_letters? } # Only letters
validates :field, format: { with: is_integer? } # Integers
validates :field, format: { with: is_decimal? } # Decimals
validates :field, format: { with: is_alphanumeric? } # Letters and numbers
validates :field, format: { with: is_email? } # Email format
validates :field, format: { with: is_phone_number? } # Phone format
validates :field, format: { with: is_url? } # URL format
validates :field, format: { with: is_zip_code? } # ZIP code
validates :field, format: { with: is_hex_color? } # Hex colors
validates :field, format: { with: is_username? } # Usernames
validates :field, format: { with: is_positive_number? } # Positive numbers
validates :field, format: { with: is_uuid? } # UUIDs
validates :field, format: { with: is_credit_card? } # Credit cards
validates :field, format: { with: is_ssn? } # SSN format
validates :field, format: { with: is_ipv4? } # IP addresses
validates :field, format: { with: is_time_24h? } # 24-hour time
validates :field, format: { with: is_date_yyyy_mm_dd? } # YYYY-MM-DD dates
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
# Validation
"12345".is_number? # => true
"abc123".is_letters? # => false
"[email protected]".is_email? # => true
"https://example.com".is_url? # => true
# Manipulation
"hello-world-123".remove_dashes # => "helloworld123"
"hello world 123".remove_numbers # => "hello world "
"abc123xyz".keep_numbers # => "123"
"abc123xyz".keep_letters # => "abcxyz"
"Hello@World#123!".remove_special_chars # => "HelloWorld123"
# Conversion
"HelloWorld".to_snake_case # => "hello_world"
"hello_world".to_camel_case # => "helloWorld"
# Extraction
"Contact: [email protected] or [email protected]".extract_emails
# => ["[email protected]", "[email protected]"]
"Price is $123 or $456".extract_numbers # => ["123", "456"]
# Utilities
"Hello world, this is a test".word_count # => 6
"This is a very long string".truncate(15) # => "This is a ve..."
# Numeric Methods
42.is_positive? # => true
-10.is_negative? # => true
100.to_currency # => "$100"
1234.to_formatted # => "1,234"
0.75.to_percentage # => "75.0%"
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.