Verbal Expressions Are Easier Than Regular Ones
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


Google Code-in 2017 An Epic Achievement
05/02/2018

The 8th annual Google Code-in contest in which teenage students learn about open source and make practical contributions turned out to be "epic" - with 3555 students from 78 countries completing  [ ... ]



Software Developer Rated Best Job Of 2018
22/01/2018

Software Developer has the distinction of coming top not only as Best Technology Job and Best STEM Job in the U.S. News ranking, but also as the best job overall for 2018. CNNMoney which also compiles [ ... ]


More News

 

Last Updated ( Tuesday, 06 August 2013 )
 
 

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