Verbal Expressions Are Easier Than Regular Ones
Written by Alex Armstrong   
Monday, 05 August 2013

This is another of those great ideas that once you have seen you can't understand why it hasn't been invented before. Verbal expressions make use of a fluent interface to specify a match for any target string. 

You know how it is. You sit and look at a regular expression and you know what all the parts mean but .... what does it do? Even if you are an expert there are occasions when you miss a symbol or the scope of a symbol. Regular expressions are dense and therefore they are error prone.

There has to be a better way.

Enter Verbal Expressions. No it doesn't do away with regular expressions, it gives you an easier way to create them. 

The idea starts out from JavaScript's ability to create fluent interfaces by chaining method calls in the style of jQuery. This trick is often used to create simple Domain Specific languages and this is what Verbal Expression is. 

You have methods like startOfLine and then and you can change them together so:

VerEx().startOfLine().then("I Programmer");

returns a regular expression that matches the string "I Programmer" at the start of a line. 

Easy! My favourite Verbal Expression method is maybe(string) which matches the string zero or once. So:

VerEx().find("ISBN").maybe(":")

generates a regular expression that finds the string "ISBN" or "ISBN:"

You can take a look at the documentation, which isn't very complete, but the way that Verbal Expressions work is fairly obvious. Notice that the VerEx static object returns a standard regex object which you can use as if you had written the regular expression manually.  

It is obvious that if you are a regular expression master then you probably aren't going to be impressed, but for the rest of us mortals it seems like a really easy way to work. The chances of understanding a Verbal Expression after a few weeks of not seeing it are much higher then a dense regular expression.

The JavaScript library can be used in the browser or in Node.js.

If you are thinking that this would be a good idea for languages other than JavaScript then you have been beaten to the post by a set of other GitHub projects for Ruby, C#, Python, Java, Groovy, PHP, Haskell and C++. 

 fig2

 

If you want to know more about regular expressions in JavaScript see Ian Elliot's article Master JavaScript Regular Expressions, or for similar help in C# see .NET Regular Expressions In Depth, and if you want to have some fun with then remember the Regular Expression Crossword Site

 

More Information

VerbalExpressions GitHub

VerbalExpressions Wiki

Related Articles

Master JavaScript Regular Expressions

Regular Expression Crossword Site

Can You Do The Regular Expression Crossword?

.NET Regular Expressions In Depth

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 

Banner


PyPy – a faster Python
26/09/2014

PyPy 2.4, aka Snow White, has been released with significant performance enhancements and bug fixes.



Build Your Own Soft Robot
27/09/2014

Soft robots are an unexpected development in robotics technology and, while they have obvious attractions, getting started building one is more difficult than buying off-the-shelf components, Now Harv [ ... ]


More News

 

Last Updated ( Tuesday, 06 August 2013 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.