How to Create Regular Expressions

In RegexMagic, a regular expression is built from fields. A field in RegexMagic is a part of the regular expression that either consists of other fields, or that matches a pattern. A pattern describes a basic piece of text for which RegexMagic can generate a regular expression. There are patterns for characters, numbers, dates, email addresses, and many more.

The simplest regular expression you can create in RegexMagic has just one field with one of RegexMagic’s patterns. Start off with by clicking the New Formula button on the toolbar at the top to clear out any leftover settings. Then you can create such a regular expression and then clicking the Add field 1 button on the Match panel. Then use the “pattern to match field” drop-down list to select the pattern you want to base your regular expressions, set the pattern’s options, and generate the regex. The email pattern example is a simple example that does this.

The other way to create a regex with a single pattern, after starting with a new formula, is to add and then select the text that the regular expression should match on the Samples panel. Then click the Mark button on the Samples panel. The new field will show up on the Match panel. RegexMagic automatically selects the pattern that best fits the text you marked. Selecting an email address, clicking the Mark button, and then the Generate button on the Regex panel is all you need to do to get a regex that matches any email address. You can find an example of building a longer regex this way in the example explaining the “pattern used by another field” pattern.

By using multiple fields, each of which can have a different pattern, you can create regular expressions that match anything you want. The most basic way to combine fields is to simply add multiple fields, one after the other, to create a regex that matches one pattern after the other. The example on SKU codes combines several basic patterns to match a specialized identification code.

When using multiple fields, you may need to repeat several of those fields as a group. You can do that by putting those fields into a sequence field, and then setting the repetition options for that sequence field. See the example explaining how to repeat combined fields using a sequence field has all the details.

Sometimes, you want to make a regex match one thing or another thing, or one of several things. In RegexMagic, you can do this by selecting “alternation” in the “kind of field” drop-down list for a field. Then you can add a field for each alternative under that field. Matching unrelated items using alternation is a basic example. Matching complex unrelated items using alternation of sequences is a more complex example.

Generating the Regular Expression

If you want to generate a replacement text so you can do a search-and-replace with your regular expression, follow the topic on creating a replacement text. When you’ve done that, or if you don’t want a replacement text, you’re ready to generate the regular expression.

Switch to the Regex panel and select the application or programming language you’ll use your regex with from the drop-down list on the toolbar on the Regex panel. This automatically selects the correct regular expression flavor. When selecting a programming language, this also selects the correct string style for the Copy button and the correct source code template for the Use panel. If your application needs to work with multiple applications or multiple versions of the same application, select the primary application or version in the drop-down list and click the Compare button to select all your target applications for comparison.

If your application supports free-spacing regular expressions, push down the Free-spacing button to get a more readable regular expression. Turn off the Free-spacing button if you want the whole regex on one line. If your application doesn’t have toggle buttons for matching modes such as “case insensitive” or “dot matches line breaks”, push down the Mode modifiers button to tell RegexMagic to use mode modifiers instead. If the Free-spacing button and/or Mode modifiers button buttons are grayed out, that means the regular expression flavor used by your application doesn’t support free-spacing or mode modifiers.

If you’re planning to generate a source code snippet on the Use panel, you don’t need to worry about the “modifiers” button. The source code snippet will automatically set the correct options, using mode modifiers or another mechanism offered by your programming language or regex library. You only need to toggle the “free-spacing” button depending on how verbose you want your source code to be.

Finally, click the Generate button to generate your regular expression. The Generate button will remain pushed down. If you make changes to the Match panel, your regular expression is automatically updated. While all examples in this help file tell you to push the Generate button at the end, you can actually push it at the start, if you want the Samples panel to highlight your regex match in progress.

You can now check the regular expression matches on the Samples panel, or put your regular expression to good use.