This is a new project, but is reasonably mature for its age.  ;)

See http://github.com/hal9000/regexador

When a regular expression grows too complex to read or maintain,
construct a small script to describe it instead.

Example from the README  (see below).

Comments welcome.

Thanks!
Hal Fulton


Suppose we want to match a string consisting of a single IP address.
(Remember that the numbers can only range as high as 255.)

Here is traditional regular expression notation:


/^(25[0-5]|2[0-4]\d|([01])?(\d){1,2})\.(25[0-5]|2[0-4]\d|([01])?(\d){1,2})\.(25[0-5]|2[0-4]\d|([01])?(\d){1,2})\.(25[0-5]|2[0-4]\d|([01])?(\d){1,2})$/

And here is Regexador notation:

    dot = "."
    num = "25" D5 | `2 D4 D | maybe D1 1,2*D
    match BOS num dot num dot num dot num EOS end

In your Ruby code, you can create a Regexador "script" or "program"
(probably by means of a here-document) that you can then pass into
the Regexador class. At minimum, you can convert this into a "real"
Ruby regular expression; there are a few other features and functions,
and more may be added.

So here is a complete Ruby program:

    require 'regexador'

    program = <<-EOS
      dot = "."
      num = "25" D5 | `2 D4 D | maybe D1 0,2*D
      match WB num dot num dot num dot num WB end
    EOS

    pattern = Regexador.new(program)

    puts "Give me an IP address"
    str = gets.chomp

    rx = pattern.to_regex    # Can retrieve the actual regex

    if pattern.match?(str)   # ...or use in other direct ways
      puts "Valid"
    else
      puts "Invalid"
    end