Verbal Expressions in Ruby
“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.”
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.
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:
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.
Scan text for URLs (example from official docs):
url = VerEx.new do
anything_but ‘ ‘
your_paragraph.scan(url) => returns array of urls found in your paragraph
Check if a file is an image (or named that way):
valid_file_name = VerEx.new do
valid_file_name.match(file_name) ? true : false
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
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!
- 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.
- VerEx would definitely be easier to understand if you already know RegEx.
- 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.