This application was originally written as a testing application for writing regular expressions for parsing the C++ grammar. Although the technique ultimately proved to not be capable of creating the very complex grammatical elements needed I thought I would release this application to the world so that others might find it useful for writing and testing regular expressions against text files.
The application’s interface contains a number of elements as follows (greater explanations are provided in the following sections):
|Toolbar||The toolbar provides the ability to create, open, save and close different regular expression files (*.regex). It also provides some basic text editing tools (Undo, Cut, Copy, Paste, etc.) along with a Find functionality (using regular expressions) and the ability to configure the editors and highlighters.|
|Search Path||The path should specify the location where the application will start it’s recursive search for files that match only one of the semi-colon separated file wildcard filters.|
|Regular Expression Editor||This editor is where the regular expressions should be written (note: they must be defined as a macro – see below for more details).|
|Results Treeview||The results treeview provides information on the results for each macro for all the files in the search path.|
|Preview Pane||Finally the preview pane will show the various information depending upon what is selected in the results treeview: Errors, Regular Expressions and Files Matches.|
The search path is relatively simple. It allows you to search recursively from either an absolute or relative path using a semi-colon separated list of file wildcard filters.
A search will automatically start after a short period of time (see configuration to change this). You can interrupt a search (if its taking too long) by pressing the <Escape> key.
Writing Regular Expressions
The regular expression extensions I’ve written into the pre-processor allow for text to contain C++ style block and line comments so that you can document your regular expressions.
All regular expressions must be defined as a macro. This is a macro name in the form
$(name) immediately followed by an equals sign then immediately followed by a regular expression which can contain zero or more macros (which must be previously defined).
To help in understanding the regular expressions you are writing I’ve created a SynEdit Highlighter for regular expressions which contains the extensions I’ve put into the pre-processor.
Also note that the regular expressions can contain keywords. To load keywords in the application place them in a file called
keywords.txt in the same directory as the executable (one is already provided with C++ keywords).
The results are shown as the path search is undertaken. They contain the name of the macro being search for in the files along with the regular expression and then a hierarchical tree view of the search results with the matches and any grouping you have defined.
Grouping MUST BE explicitly defined using a name for it to appear in the list of groups else you will only have the default first group which is the match of the whole text. Additionally, you can hide a macro from being search for (but still allow it to be nested within another macro) by pre-pending the name of the macro with an
The tree has a context menu which allows you to copy any selected node. This is useful for getting the raw resultant regular expression for a macro.
When you select any of the search results the selected regular expression is highlighted in the top regular expression editor. Depending upon what you select the preview editor will show a number of things. If you select a file, match or group, the the preview will show you that file and the matching text you have selected.
If you select the regular expression then the preview editor shows you a broken down view of the regular expression with indentation and parenthesis line breaks to help you more easily view the resultant expression so that you can debug any expression that is not working.
The toolbar across the top of the application allows you to do a number of things (note: this application works on the principle of a Single Document Interface (SDI)).
|New||This will create a new blank regular expression file to work on (you will be prompted to save any existing changes to a previous file).|
|Open||This allows you to open an existing regular expression file (*.regex) to work on (you will be prompted to save any existing changes to a previous file). Note: DGHRegEx will automatically load the last file you worked on when restarted.|
|Save||The allows you to save changes to the file you are working on. If this is a new file you will be prompted for the location and name of the file.|
|Save As||This allows you to save the current file to a new file – you will be prompted for the location and name of the file.|
|Close||This will close the currently open expression file you are working on (you will be prompted to save any existing changes to the file).|
|Exit||This closes the application (you will be prompted to save any existing changes to the file).|
|Undo||This option undoes the last change in the editor.|
|Cut||This cuts the selected text from the editor.|
|Copy||This copies the selected text from the editor.|
|Paste||This pastes text from the clipboard into the editor at the cursor position.|
|Delete||This deletes the selected text in the editor.|
|Select All||This selects all the text in the editor.|
|Find||This displays a find dialogue wherein you can search for text using regular expressions.|
This dialogue has 3 tabs for configuring the selected editor and highlighter as follows:
This page of the dialogue allows you to change various visual aspects of the editor.
This page of the dialogue allows you to change the behaviour of the editor.
Finally this page of the dialogue allows you to configure the font styles and colours of the syntax highlighter currently associated with th active editor.
|Update Interval||This allows you to change the update interval (in seconds) between the last sarch path change or regular expression editor change and the start of a new search.|
You can download the application (built with RAD Studio Berlin 10.1) and the source code from the below links (You will need VirtualTreeView and SynEdit components to compile the project):
- Application and source code: DGH Regular Expressions 1.0;
- Source code (GitHub): https://github.com/DGH2112/DGH-Regular-Expressions.