Regular expressions can be a powerful tool in searching through strings or documents. If you haven't used them before, think of them as a string pattern matching tool on steroids.
MATLAB introduced support for regular expressions in MATLAB 6.5 (R13). You can read about regular expressions in MATLAB here. There are also a couple of blog posts on Loren Shure's Art of MATLAB here and here.
Despite their usefulness, the drawback to regular expressions has always been their non-intuitive syntax. For example, how do you find all strings that start with a capital "B" and end with "e"? Here is one option:
regexp('Bobbie was born on Bastille Day before dawn.', 'B[a-z]*e')
Now what if I want to ignore case? Or allow spaces within the string? Regular expressions can handle all these cases, but it takes some experience, and some doing.
Michael's regexpBuilder is an app that allows the user to interactively build their regular expression and see the results in real time. It even has checkboxes for some of the more common tasks (e.g. "ignore case", or "match once"). This can help reduce the time (and frustration) needed to construct the desired regular expression.
The user interface is quite simple: enter the text-to-be-searched in the big "Text" box, then start entering a regular expression in the top left "Regexp" box. The tool displays the results by highlighting and/or underlining the matches in the text window, but it also goes further: it lists all the different outputs from MATLAB's regexp function on the right-hand side of the screen. And just to make things a little more convenient, when you press the "Evaluate" button, it also echoes the equivalent MATLAB command at the MATLAB command prompt.
Suggestions for improvements
This app accomplishes its advertised task quite well. Here are some ideas on what could be added:
- The user interface could be cleaned up to look more professional
- The text boxes on the right aren't supposed to be used by the user, and should probably be locked so the user doesn't type in them.
- Ability to import text files and/or web page would be nice (as in this FileExchange submission).
As always, your thoughts and comments here are greatly appreciated.
Get the MATLAB code
Published with MATLAB® R2013b