Project

irregular

0.0
No commit activity in last 3 years
No release in over 3 years
Module providing a method to compile regular expressions from templates.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.7
~> 10.0
~> 3.4
 Project Readme

Irregular

Regular expressions are very powerful, but they can get large and archaic quickly. Irregular provides tools that work together to help you abstract your regular expressions into human-readable code.

Irregular-rb comes with:

  • A templating language for Regular Expressions that helps you abstract each chunk into a human-readable string

irregular-rb is based on irregular-js.

Usage Example

class CatRegexp
  include Irregular

  def cat_names
    names = ['betty', 'joyce', 'franklin', 'peter']
    "(#{names.join '|'})"
  end
end

irregular = CatRegexp.new

# insert results of method calls using backticks
regexp = irregular.compile_regexp(/I love my cat `cat_names`!/i)
p regexp # => /I love my cat (betty|joyce|franklin|peter)!/i

# use your regexp as normal
regexp =~ "I love my cat Joyce!" # => 0

Installation

Add this line to your application's Gemfile:

gem 'irregular'

And then execute:

$ bundle

Or install it yourself as:

$ gem install irregular

API

Irregular

A module that provides functionality for compiling Irregular Expressions. Does nothing on its own; include or extend it into your class/module.

Irregular.compile_regexp

compile_regexp(regexp, options = 0)
compile_regexp(string, options = 0)

Method that compiles an irregular expression into a Regexp. (returns Regexp)

Parameters

regexp

A regular expression to use the source pattern and options of for compilation.

string

A string to use as the source pattern for compilation.

options

A list or or-ed Regexp options to apply to the resulting Regexp.

If passed with a Regexp object, it will be or-ed against the Regexp's existing options.

Contributing

Pull requests, bug reports welcome.

This library tries to mirror compilation behavior of irregular-js as much as possible/needed- if you notice behavior you think might be a bug, test if it behaves the same in irregular-js first. If you still think it's a bug, the report should be filed against irregular-js, and then once it is resolved there, a bug report should be filed here.

Notes

This library doesn't implement irregular-js's named captures functionality because named captures are supported natively in Ruby (they aren't in ES5).

Running tests

Tests are written using Rspec. To run:

$ bundle install rspec spec

License

MIT