File Exchange

image thumbnail

Interactive Regular Expression Tool

version (5.04 KB) by Stephen Cobeldick
Develop and refine regular expressions in an interactive figure that shows all REGEXP's outputs.


Updated 22 Aug 2017

View License

This is a simple interactive tool to help develop regular expressions. It lets you interactively write regular expressions and see REGEXP's outputs update as you type.

This submission consists of just one M-file which creates a figure containing two edit-boxes (to allow editing of a parse string and a regular expression string) and displays all of REGEXP's outputs inside the figure.

This function can be used in two ways (and in any combination of these two ways):
1. interactively by typing directly into the figure's edit-boxes, or
2. by calling the function with exactly the same inputs and options as REGEXP itself.

Simply keep the figure open to use any combination of these two modes of operation: this allows for refining of the regular expression by changing the parse and regular expression strings, and also updating any REGEXP options that are supplied as function inputs. Text can also be copied and pasted to and from the two edit-boxes.

### Keep It Simple ###

This is intended to be the simplest interactive regular expression editor on FEX: no highlighting, no links, no colors, no buttons, no java code, no file parsing, no apps to install, no third-party dependencies... just this one M-file is all that is needed. IREGEXP allows fast interactive development of a regular expression, especially by allowing editing using only the keyboard (use the tab key to switch between the edit-boxes), to parse short pieces of text.

### Important ###

MATLAB's UICONTROL text edit-boxes do not update as one types, so I used a little keypress-hack to try and achieve this. This hack does get easily confused, so to ensure that the correct text is being parsed it is necessary to enter the text properly: clicking away from the edit-box, as per MATLAB's UICONTROL documentation, is the only way to guarantee this!

Also note that in multiline mode the edit-box itself removes trailing newline characters from the text (not my code!).

### Example ###

Ellipses indicate work continuation without closing the figure window.

% Call with default parse and expr strings:
% ...interactively add the parse string '0_AAA111-BB22.CCCC3'.
% ...interactively add the regular expression '([A-Z]+)'.
% to set a new parse string:
% ...interactively change the regular expression to '([a-z]+)\d+'.
% to allow case-insensitive matching:
% ...interactively change the regular expression to '(?<str>[A-Z]+)(?<num>\d+)'.
% to assign selected outputs to some variables:
[A,B,C,D] = iregexp([],[],'match','start','tokens','split','ignorecase')
A = {'aaa111', 'BB22', 'cccc3'}
B = [3, 10, 15]
C = {{'aaa','111'},{'BB','22'},{'cccc','3'}}
D = {'0_','-','.',''}

Comments and Ratings (5)


Very helpful if you don't use regexp often enough to remember the tricks.

@KE and Kouichi: thank you both for your feedback. Unfortunately I have an older (non-HG2) version of MATLAB, so I cannot test my code with post 2014b versions.

Based on your comments I will make a few changes soon though, that should make this submission compatible with MATLAB 2014b and later versions.

@Kouichi: I did a small survey of friends and family, and it was inconclusive: some people I asked wanted the inputs at the bottom, some at the top, and everyone had a different explanation of *why* they had that preference...


Needed the fix below


Apparently, you may have a different opinion, but thought it might be slightly more intuitive to have "ParseString" and "MatchExpression" at the top. This is jut a vote. :)


Hi Stephen,

This is a really useful tool. You can see the what kind of expression matches what in real time. This could save my time a lot and also help my understanding of regular expression. Thanks a lot.

I tested it on R2016b-pre, but the code did not run until I made a couple of modifications below. hnd is a uipanel object and does not have 'TitleHandle' property. Also 'Title' property of a uipanel does not allow you to turn it into a checkbox.

It would be nice if you could consider including the following conditional block or otherwise to make it compatible with the latest releases.

if verLessThan('matlab','8.4.0')
%% R2014a or earlier
%% R2014b or later


line 89
[fig,out,seq,par,chk] = rgxNewFig(inp); %chk needed to be deleted
line 101
% set(chk,'Value',true); % commented out

line 135
function [fig,out,seq,par,chk] = rgxNewFig(inp) %chk needed to be deleted

line 214
old = warning('off','MATLAB:Uipanel:HiddenImplementation');
chk = get(hnd,'TitleHandle'); % Thank you to Yair Altman for the idea.
set(chk, 'Style','checkbox', 'Position',get(chk,'Position')+[0,0,18,0],...
'Value',0, 'Callback',@(H,E)set(par(1),'Max',1+(get(H,'Value'))));
% Commented out

line 221
set(par(1),'Max',1) % Single-line parse-string. %included


* rename function.
* try to make HG2 compatible (needs testing).
* simplify internal handling of output arguments.

* Display outputs in edit boxes (as these have scrollbars).
* Input string can change ParseString edit box to multi-line.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor