Verbal Expressions in Ruby

/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
“The only person who understands a regular expression is the one who wrote it, and only for the 30 minutes right after they wrote it.”
-Some Developer

Let’s be honest — if you’re not an expert, regular expressions look like someone passed out on their keyboard.

Luckily, there’s a gem for that. It’s called VerEx, short for Verbal Expressions. VerEx lets you write regular expressions with code that’s closer to English.

The verbal expressions library was first released in JavaScript two years ago and the wonderful developer community has been kind enough to port it over to some other languages, including Ruby.

If you check out the Ruby Verbal Expressions documentation, you’ll see that just like with normal RegEx, VerEx lets you search for pattern matches using intuitively-named methods like #anything_but, #maybe, #range, etc.

Adding Verbal Expressions to your project:

Like any gem, to add Verbal Expressions to your project you need to install it:

gem install verbal_expressions

and require it:

require "verbal_expressions"

and then you’re good to go.

This gives you access to the VerEx class and its associated methods that can help you find and match expressions. You write your verbal expressions and then VerEx translates that to real regex behind the scenes.

Some Examples

Scan text for URLs (example from official docs):

url = VerEx.new do
find ‘http’
maybe ‘s’
find ‘://’
maybe ‘www.’
anything_but ‘ ‘
end
your_paragraph.scan(url) => returns array of urls found in your paragraph

Check if a file is an image (or named that way):

def is_valid_img_file?(file_name)
file_name.downcase!
valid_file_name = VerEx.new do
anything
find “.”
find “jpg”
alternatively
find “jpeg”
alternatively
find “tiff”
alternatively
find “bmp”
end

valid_file_name.match(file_name) ? true : false
end
is_valid_img_file?("cats.jpg") => true

VerEx even lets you see the RegEx it’s generating from your code:

valid_file_name.source => (?:(?:.*)(?:\.)(?:jpg))|(?:(?:jpeg))|(?:(?:tiff))|(?:(?:bmp))

You can also use verbal expressions to replace text snippets that match your expression:

day_end = VerEx.new do
maybe ‘ur’
maybe 'nes'
find ‘day’
end
sentence = “It’s Friday, which means tomorrow is Saturday and it is the weekend!”
result = sentence.gsub(day_end, ‘.’)
result => It's Fri., which means tomorrow is Sat. and it is the weekend!

My Take

  1. VerEx is pretty nifty! It takes longer to write than normal RegEx, but probably introduces fewer bugs into your code. It’s also easier to read and debug, which matters when working with other developers.
  2. VerEx would definitely be easier to understand if you already know RegEx.
  3. VerEx could use some better documentation and examples online…a good opportunity for someone to add to the knowledge base. I plan to…once we cover RegEx.
  4. If you plan to use the Ruby library and you have questions, check out the JavaScript documentation for more examples.