Apps

Save time by testing regular expressions with Expresso


This article is also available as a TechRepublic download.

Learning how to write regular expressions is a little like sex education: Studying theory is great, but it pales in comparison to the real thing. Face it, there is a world of difference between theory and practice in both activities, and the best way to learn something is by doing. Say, for example, that you want to see what works for you; wouldn't it be nice to try it beforehand and see what happens? Maybe that's why my interest was aroused when I discovered the free regular expression development tool, Expresso.

For the uninitiated, Expresso is a development tool for the testing of regular expressions. Yes, no longer is it necessary to play around with a regular expression in the actual application to see what clicks. Now it is possible to try something out with no strings attached. Don't like how it works? Try something different.

Like riding a bicycle

You never forget your first regular expression. Yes, the years may somewhat dim the memory, but the rush is unforgettable. However, since some might be inexperienced, I suggest looking over Table A to familiarize yourself with the syntax.

Table A

Regular expression syntax

Pattern matching

All kidding aside, regular expressions are used for pattern matching, where some text is matched against a pattern and an action is then taken. The action taken when a match is found could range from merely indicating that a match was found to isolating the matches to replacing the matching text. Let's explore some of the possibilities using the text show in Listing A.

Listing A

Sample text (excerpt from Edgar Allen Poe's The Raven)

Once upon a midnight dreary, while I pondered weak and weary,

Over many a quaint and curious volume of forgotten lore,

While I nodded, nearly napping, suddenly there came a tapping,

As of some one gently rapping, rapping at my chamber door.

`'Tis some visitor,' I muttered, `tapping at my chamber door -

Only this, and nothing more.'

Ah, distinctly I remember it was in the bleak December,

And each separate dying ember wrought its ghost upon the floor.

Eagerly I wished the morrow; - vainly I had sought to borrow

From my books surcease of sorrow - sorrow for the lost Lenore -

For the rare and radiant maiden whom the angels named Lenore -

Nameless here for evermore.
Let's say that we're interested in knowing how many times the name "Lenore" is used in the above text. The regular expression for this would be "lenore" for case-insensitive tests or "Lenore" for case-sensitive tests. While I'm on the subject, I should mention that the options are set using check boxes under the Options tab (Figure A).

Figure A

Regular expression options tab
Along the same lines as the Options tab, there is the Replacement String tab, where not surprisingly, the replacement String goes, as Figure B shows.

Figure B

Regular expression replacement string tab

Of course, the majority of regular expressions written don't usually spell out exactly what you're looking for. Consider U.S. ZIP codes (postal codes). The acceptable formats are either five numeric digits or five numeric digits, dash, four numeric digits. So testing a string that is only the ZIP code, we have is one pattern for the five digit, "^\d{5}$", and a second, "^\d{5}-\d{4}$" for nine digits. Putting both of these expressions together gives us "^\d{5}(-\d{4}$)?", which pretty much gets the job done.

Features

The Expresso features I find myself using the most are match (Figure C) and replace (Figure D). Yeah, I know there are more, but these were enough to attract my attention. This doesn't mean that the other features are anything to sneeze at. Just take a look at the list below and imagine the possibilities.
  • Build regular expressions by selecting prewritten code snippets.
  • Test against text that can be entered manually or loaded from a file.
  • Display matches in a tree structure.
  • Highlight the matching text in the source document.
  • Check for syntax errors in regular expressions automatically.
  • Generate VB, C++, or C# code.
  • Read and write regular expressions.
  • Maintain a history of regular expressions.
  • Manage a regular expression library.
  • Provide the ability to optimize regular expressions.
  • Translate regular expressions into precise English.
  • Perform matches using only part of a regular expression.

Figure C

Match

Figure D

Replace

Worth it

If you're one of those developers, like I was, who found regular expressions alluring but didn't want the commitment of a longterm testing relationship, consider using Expresso. It is free to try and free to use unregistered for a limited period. The registration is both free and easy. If you want to continue using it after the trial period, all that is required is a name and an e-mail address.

0 comments