Build Status Gem Version


Verbal is a Ruby library that helps to construct difficult regular expressions. It's ported from the awesome JavaScript VerbalExpressions. Detailed documentation is available at rubydoc.


$> gem install verbal
require 'verbal'


Here's a couple of simple examples to give an idea of how Verbal works:

Testing if we have a valid URL

# Create an example of how to test for correctly formed URLs
tester = do
  find 'http'
  maybe 's'
  find '://'
  maybe 'www.'
  anything_but ' '

# Create an example URL
test_url = ""

# Use it just like a regular Ruby regex:
puts 'Hooray!  It works!' if tester.match(test_url)
puts 'This works too!' if tester =~ test_url

# Print the generated regex:
puts tester.source # => /^(?:http)(s)?(?::\/\/)(www\.)?([^\ ]*)$/i

Replacing strings

# Create a test string
replace_me = "Replace bird with a duck"

# Create an expression that seeks for word "bird"
expression = { find 'bird' }

# Execute the expression like a normal Regexp object
result = replace_me.gsub( expression, "duck" );

puts result # Outputs "Replace duck with a duck"

Capturing strings

# create expression
verbal = do
  capture { anything }
  find /\sby\s/
  capture { anything }
# match against test string
data = verbal.match('this is it by michael jackson')
puts data[1] # >> 'this is it'
puts data[2] # >> 'michael jackson'


  • I haven't yet ported the modifier code because Ruby Regexp handles modifiers a little differently.


Thank you to @jehna for coming up with the awesome original idea.

Thank you to @ryan-endacott for the original port.