Source Code Template Editor

RegexBuddy makes it easy to generate source code snippets for all of the world’s most popular programming languages. Each language template implements a comprehensive set of functions, or things that you can do with your regular expression in the selected programming language.

If the provided functions don’t match your coding style, you can easily edit the template for the current language by clicking the Edit button on the toolbar on the Use panel. You can edit the template for any language by clicking the Edit button in the application selection window. You can edit or delete the provided functions, and add as many new functions as you like.

You can also create new templates. These can be alternative sets of functions for a programming language already supported by RegexBuddy, such as a set of functions specific to a particular development project. You can also create templates for languages not supported by RegexBuddy. The only limitation is that you’ll be limited to languages that use one of the regex flavors and string styles already supported by RegexBuddy. To create a new template, simply use the Save As button in the template editor. To use the new template, add a custom application and select the new template for the custom application.

RegexBuddy Source Code Template Editor

The RegexBuddy Source Code Template Editor appears when you click the Edit button on the toolbar on the Use panel. To start with a fresh template, click the New button. To open an existing template, click the Open button and select the template from the drop-down list. Click the Save button to save your modified template, overwriting the existing template. Click the Save As button to save the template under a new name, so both the original and the modified template will be available.

Templates that ship with RegexBuddy are stored in the same folder as RegexBuddy itself. The default installation folder is “C:\Program Files\Just Great Software\RegexBuddy 4”. Templates that you save are always saved under the “Application Data\RegexBuddy 4” folder under your Windows user profile. If two templates with the same file name exist in both folders, only the template in your user folder is used. The effect is that if you open a template included with RegexBuddy and save it with the same name, only the modified template is available in RegexBuddy, but the original template remains available on disk. When you install new versions of RegexBuddy that ship with updated templates, the installer overwrites the templates in RegexBuddy’s installation folder and leaves the modified templates in your user folder alone. RegexBuddy keeps on using your customized templates.

General

On the General tab in the template editor, you should set the general properties of the language you’re creating a template for. You should also enter your contact details, in case you want to share your template on RegexBuddy’s user forums.

Language: Enter the name of the programming language the template is for. You can specify a more descriptive purpose if you’re creating an alternative template for a language that already has a template. This name will appear in the Language drop-down list on the Use panel.

Regex and replacement flavor: Select the regular expression flavor and the replacement text flavor used by the language. The available replacement text flavors depend on the regular expression flavor that you selected. It is not possible to define your own flavors. The flavor you select is the target flavor that RegexBuddy will use when converting your regular expression.

String style: Select the kind of string or operator that the language uses to specify literal regular expressions in source code. The %REGEX% and %REPLACEMENT% placeholders insert the regular expression and replacement text into the generated source code snippet after formatting it using the string style you select here. This way, RegexBuddy automatically takes care of placing quotes around the string and escaping quotes and backslashes in the regex, or whatever the selected string style requires. The available string styles are the same ones available via the Copy and Paste buttons on RegexBuddy’s main toolbar.

Syntax coloring scheme: Select the syntax coloring scheme that RegexBuddy should use to highlight keywords, comments, strings, etc. when displaying your source code snippet on the Use panel. This only affects how your code snippets are displayed in RegexBuddy. It does not alter the code snippet if you copy and paste it into a plain text editor or development tool. The highlighting is preserved if you copy and paste into a word processor or rich text editor. Click the Download button to download coloring schemes shared by other RegexBuddy users. Click the Edit button to edit the selected syntax coloring scheme with the syntax coloring scheme editor. The editor is an external application that licensed RegexBuddy users can download for free from the EditPad Pro web site. RegexBuddy uses the same syntax coloring system as EditPad Pro.

Single-line comment delimiter: Type in the character or characters that start a comment that runs until the end of the line in the language you’re creating the template for. Leave this box blank if the language doesn’t support single-line comments.

Multi-line comment delimiters: In the box at the left, type in the character or characters that begin a multi-line comment. In the box at the right, type in the characters that end a multi-line comment. Leave both boxes blank if the language doesn’t support multi-line comments.

You have to specify either the single-line comment delimiter or the multi-line comment delimiter pair for RegexBuddy to be able to generate regex tree functions. If your language supports both single-line and multi-line comments, specify both styles. RegexBuddy then chooses the comment style best suited for each situation.

XML comment delimiters: If your programming language supports XML comments or documentation comments that enable the automatic generation of documentation from your source code, then you should specify those in the 3 boxes. In the left hand box, enter the line of text that starts a documentation comment. In the middle box, enter the characters that each line in the comment should be prefixed with. In the right hand box, enter the line of text that ends a documentation comment. If your language doesn’t use some of these, simply leave those boxes blank. If your language doesn’t support documentation comments at all, leave all 3 boxes blank. Then RegexBuddy won’t be able to generate the regex tree functions that use XML comments.

General settings for the Java template

You may have noticed that there is no way to specify the regular expression flavor that your programming language uses. That is correct. As explained above, source code templates are put to use by selecting them for an application. The application settings determine which regex flavors are used with which source code templates. This allows you to use multiple regex flavors with the same template, or have multiple templates for the same regex flavor.

Among the built-in applications, PCRE is a good example of how flexible this setup is. The application settings for PCRE 7.9 and prior all use a different regex flavor (specific to each PCRE version), and all share the same template “PCRE 7.9 and prior using UTF-8”. PCRE 8.00 to 8.21 also all use a different regex flavor, sharing a different template “PCRE 8.00 and later using UTF-8”. The function to iterate over all matches is coded differently in this template to take advantage of new functionality added in PCRE 8.00. Then, for PCRE 8.30 and later there are multiple application definitions that share the same PCRE 8.30 regular expression flavor, but use different templates. “PCRE 8.00 and later using UTF-8” uses the classic pcre functions, while “PCRE 8.30 and later using UTF-16” uses the new pcre16 functions and “PCRE 8.32 and later using UTF-32” uses the even newer pcre32 functions.

Modes

On the Modes tab in the template editor, you need to specify the code that RegexBuddy needs to use to make sure your code snippet uses the same matching modes as RegexBuddy itself. The labels on the Modes tab correspond with the choices in the drop-down lists on RegexBuddy’s toolbar.

What you specify on the Modes tab is not automatically inserted into the source code snippets. Rather, the code you specify for each mode is used to build up placeholders such as %MODELIST%, %CASELESS%, or %FREESPACING%. You can find the full list of placeholders in the Function section below.

Code to turn on (opposite) modes: If your programming language supports a particular mode and requires code to be generated to turn on that mode, enter that code into the corresponding box. If your programming language doesn’t support a particular mode or doesn’t require code to be generated (because the mode is on by default), then leave that box blank. There are two columns with modes that are each other’s opposites. The “code to turn on modes” columns lists the modes that traditionally require code to be generated to turn on the mode. The “code to turn on opposite modes” lists the modes that are the traditional defaults and are thus usually left blank.

Code to set the line break handling: If the regex engine in your programming language can handle different line break styles, specify the code that activates each line break handling mode that the language supports. Leave the default mode blank unless your language requires that the line break style is always specified. Leave all boxes blank if your language does not support multiple line break handling modes.

Code to make a list of modes: If you want to use the %MODELIST% placeholder in your code snippets to insert all modes together in one spot, you can specify how RegexBuddy should build that list. If you leave all the boxes blank and the checkbox off, RegexBuddy will simply concatenate all the modes without any spacing between them. In the “begin list” and “end list” boxes you can specify which code, if any, RegexBuddy should place at the start or the end of the list. The begin and end code is only used if the list contains at least one mode. In the “delimit modes” box, you can type the characters that RegexBuddy should place between two modes. If you want the delimiter to include a line break, turn on the line break checkbox. The delimiter is only used if the list contains two or more modes. In the “empty list substitute” box you can type the code that %MODELIST% should insert when all modes are set to their defaults (i.e. all the relevant code to turn modes on and off is blank).

Modes for the PCRE template

Functions

The Functions tab is where the real action is. Here, you’ll define all the functions that appear in the Functions drop-down list on the Use panel when your template is selected.

To add a function, click the New button. The first function you add will be blank. The next function you add will automatically take over everything from the selected function, except its description. This way you can easily create different variants of a particular function.

The order in which the functions are listed in the template editor is also the order they’ll have in RegexBuddy. RegexBuddy doesn’t sort them alphabetically. You should put related functions next to each other. You can rearrange the functions with the Move Up and Move Down buttons. You can delete functions with the Delete button.

The function’s description is the label it will have on the Use panel in RegexBuddy. The Function Description drop-down list in the template editor lists all the function descriptions in all other RegexBuddy source code templates that you haven’t used yet in the template you’re editing now. This way you can easily make your function descriptions consistent between various templates. It’s also a handy reminder of the functions your template doesn’t support yet. Of course, you can always type in a new function description. If you then edit another template, the new description will also appear in the drop-down list.

The Match, Replace and Split checkboxes determine whether the function is available on the Use panel when these modes are active. You’ll need to select at least one of them. Since a comprehensive template will have a lot of functions, you can reduce clutter by restricting search functions to the Match mode, search-and-replace functions to the Replace mode, and split functions to Split mode, where they’re relevant.

Each function can have up to four parameters that the user of your template can provide. You should use these parameters for your code snippet’s input and output variables. Such variable names tend to change whenever a code snippet is used. Allowing the variable name to be set in RegexBuddy before the snippet is generated is easier than changing it after pasting the snippet into your source code editor.

For each parameter, you can specify a label in the drop-down list at the left, and a default value at the right. The drop-down list shows all function labels that you’ve already used in this template. If you select a parameter from the drop-down list, RegexBuddy also sets the default value you previously set for that parameter. If you change the default value, it changes throughout the current template for all parameters with the same label. There is no limit to the number of different parameters you can use in a language template. However, each function can use only four.

Using three checkboxes you can specify if RegexBuddy should modify the regular expression before inserting it into the function’s source code. Turn on “prepend mode modifiers to regex” if you want RegexBuddy to put mode modifiers for all the matching modes at the start of the regex. As explained in the above section about modes, RegexBuddy normally only prepends the mode modifiers for those modes for which you did neither specify “turn on” nor “turn off” code. If your programming language has certain regular expression functions that allow modes to be set in source code, while others don’t (leaving mode modifiers as the only way), you should turn on the “prepend mode modifiers to regex” for those functions that don’t. For the functions that do, you’ll use placeholders like %MODELIST%. Note that some regex flavors, like JavaScript, POSIX and XML don’t support mode modifiers. For these flavors, the “prepend mode modifiers” checkbox has no effect.

There’s a difference between checking whether a regular expression matches part of a string, versus checking if it matches the whole string. Some programming languages have separate routines for these two tasks. Other languages don’t have a special method to check whether a regex matches a string entirely. For those languages, you can create a function that checks if a regex matches a string entirely by turning on the options to prepend and append an anchor. RegexBuddy automatically uses the anchors appropriate for your language’s regular expression flavor.

Finally, you’ll need to type in the actual code snippet. The box in the lower right corner is where you type in your snippet using placeholders. RegexBuddy shows a preview of the source code it generates in the lower left corner. The preview uses the regular expression and options you used in RegexBuddy before opening the source code template editor.

You can insert placeholders by typing them in, or by clicking the Placeholders button and selecting one from the list. The following placeholders are available:

The regex and replacement tree functions have dedicated placeholders that pretty much generate the entire function. See the list of regex tree functions for more details.

You can also use conditionals to include certain code in your snippet only under certain circumstances. This is particularly useful to create snippets that call functions with optional parameters. For example, typing “%IFSPLITLIMIT%, %SPLITLIMIT%%ENDIFSPLITLIMIT%” will insert a comma, space and the split limit only when the split limit is not blank. The following conditionals are available:

Functions for the C# template