Index ¦ Archives ¦ RSS

Introducing Verbal, a Ruby Gem

I recently released Verbal, a ruby gem that provides a fluent DSL for creating regular expressions. It's a fork of jehna's popular VerbalExpressions javascript library. Detailed documentation and code examples are available at rubydoc.info.

To create a regular expression that matches http, https, ftp, and sftp, one can use Verbal as follows:

verbal = Verbal.new do
  find 'http'
  maybe 's'
  find '://'
  otherwise
  maybe 's'
  find 'ftp://'
end

Note that verbal just a regular expression.

verbal =~ 'http://' # => 0

Capturing groups may be specified using capture. For example,

verbal = Verbal.new do
  capture { anything }
  find /\sby\s/
  capture { anything }
end
data = verbal.match('this is it by michael jackson')
data[1] # => "this is it"

More examples can be found in the RSpec specs.

© James Lim. Built using Pelican. Theme by Giulio Fidente on github.