File Exchange

image thumbnail

Interactive Regular Expression Tool

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


Updated 07 May 2021

View Version History

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.

The tool consists of just one M-file which creates a figure containing two edit-boxes (for the parse string and a regular expression string) and that displays all of REGEXP's outputs inside the figure (note that the column widths can be adjusted manually).

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 be copied and pasted both to and from the two edit-boxes. If required, REGEXP's output values can be returned by calling the function with the appropriate output specifiers (by default there are no function outputs).

### Keep It Simple ###

This is intended to be the simplest interactive regular expression tool 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 you need. IREGEXP aids fast 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 you type, 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 you will need 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 continuation without closing the figure window.

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

Cite As

Stephen Cobeldick (2021). Interactive Regular Expression Tool (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Alessandro Togni

Xingwang Yong

Good job, thank you!


Great tool, very helpful.

Stephen Cobeldick

@Zhuohe Liu: good catch! I have added your suggested fix will upload a new version.
It appears that R2019a/b changed the scoping rules slightly, as this syntax used to work without error.

Zhuohe Liu

In r2019b, add a line after line 149 'out = []; ' to run.
Error:File: iregexp.m Line: 163 Column: 13
Identifier 'out' is not a function or a shared variable. To share 'out' with nested function, initialize it in
the current scope. For more information, see Sharing Variables Between Parent and Nested Functions.

madhan ravi

Raymond Wollenberg

Really helpful to come around with setting up your inputs for regular expressions!!!


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

Stephen Cobeldick

@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

Kouichi C. Nakamura

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. :)

Kouichi C. Nakamura

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

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!