Generate Source Code to Use a Regular Expression

Once you have created and tested a regex action, you are ready to use it. One way is to simply tell RegexBuddy to copy the regular expression to the clipboard, so you can paste it into tool or editor where you want to use the regex. If you do this often, you may want to see if it is possible to integrate RegexBuddy and the software you use regular expressions with.

If you want to use the regular expression in the source code for an application you are developing, switch to the Use panel in RegexBuddy. RegexBuddy can generate code snippets in a variety of programming languages that have built-in support for regular expressions, or that have a popular regular expression library available for them.

Benefits of RegexBuddy’s Code Snippets

Using RegexBuddy’s code snippets gives you several major benefits. You don’t have to remember all the details on how to use a particular language’s or library’s regex support. Though most languages and libraries have similar capabilities, the actual implementation is quite different. With RegexBuddy, you simply select the task you want to accomplish from the list, and RegexBuddy generates source code you can readily copy and paste into your code editor or IDE.

Another key benefit is that when using a regular expression in a programming language, certain characters need special treatment, up and above the special treatment they may need in the regular expression itself. The regex \\ to match a single backslash, for example, is automatically inserted as "\\\\" in a Java or C code snippet, and as /\\/ in a JavaScript or Perl snippet. Never again mess around with pesky backslashes!

For languages that support exception handling, RegexBuddy’s code snippets also contain try..catch or equivalent code blocks to handle any exception that could be thrown by any of the methods the code snippet calls.

How to Generate a Code Snippet

To generate a code snippet, first select your programming language from the list of applications in the top left corner. Then select the type of function you want to implement. Some functions are only available for certain languages. The functions that create an object for repeatedly applying a regular expression, for example, are only available in languages with object-oriented regex libraries. No functions are available for applications that aren’t programming languages.

The available functions also depend on the kind of action you defined. Search and extraction functions are listed for match actions, search-and-replace functions are listed for replace actions, and splitting functions are listed for split actions.

Most functions allow you to specify certain parameters, such as the variable or string constant you want to use as the subject for the regex operation, the variable to store the results in, the name of the regex or matcher object, etc. RegexBuddy does not verify whether you enter a valid constant or variable. Whatever you enter is inserted into the code snippet unchanged. The parameters you enter are automatically remembered. For each parameter, the same values are carried over across all the functions. The parameters are remembered separately for each language. So you need to enter the names you typically use only once.

RegexBuddy keeps the code snippet in sync with your regular expression at all times, even when the regex is syntactically incorrect. Use the Create and Test panels to make sure your regex works correctly with your programming language. The snippet on the Use panel is ready for copying and pasting at all times. If you want, you can edit the snippet in RegexBuddy. Just remember that your changes are lost as soon as you edit the regular expression.

You can transfer the snippet into your code editor or IDE by clicking the Copy button on the Use panel’s toolbar. This copies the entire snippet to the Windows clipboard, ready for pasting. If you only want to use part of the snippet, either select it and press Ctrl+C on the keyboard to copy it, or select it and then drag-and-drop it with the mouse into your code editor.

PHP code snippet to search and replace through a string

Available Languages

RegexBuddy ships with source code templates for all of the world’s most popular programming languages. Each 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 any of the templates by clicking the Edit button on the toolbar on the Use panel. 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 use a new template, first save the new template in the template editor. Then add a custom application and select the new template when adding the application.