How to Use Your Regular Expressions

Just as RegexMagic makes it easy to create regular expressions, RegexMagic makes it easy to put your regular expressions to use. If you want to search through files and folders, simply switch to the GREP panel in RegexMagic, specify the folder and file masks, and search.

To use your regular expression in another application, click the Copy button on the Regex panel and copy the regular expression “as is”. This copies the regular expression as it appears on the Regex panel in RegexMagic, which is how you should paste it into the Search box of an application that supports regular expressions. Also make note of the “required options”, if any, that you see on the Regex panel. You’ll need to set the same options in the application you’re pasting your regex into. If you don’t, the regex will not work as intended. You can click the Modifiers button (purple tick mark) on the Regex panel if you want a self-contained regex that does not depend on any options. The Modifiers button is disabled for applications that don’t support mode modifiers.

If you want to use your regular expression in the source code of a script or application you’re developing, the best way is to generate a source code snippet on the Use panel. When you select your programming language on the Use panel, RegexMagic automatically selects the correct regex flavor for that language on the Regex panel. The generated source code snippet will have your regular expression properly formatted as a literal string or regex. The source code snippet automatically sets the “dot matches line breaks”, “case insensitive”, and “^ and $ match at line breaks” options as needed. If your programming language or the function you’re using doesn’t have flags or properties to set those options, RegexMagic automatically adds mode modifiers to the regex, even if you don’t turn on that button on the Regex panel.

Essentially, the Use panel does everything needed to generate a code snippet with the exact same regex that you have in RegexMagic. The most common reason for complaints that “it works in [some regex tool] but not in [some programming language]” and vice versa is that people don’t copy and paste the regex correctly, or forget to set the correct options. Save yourself the hassle and use the Use panel.

If you don’t like RegexMagic’s source code snippets because they don’t fit your coding style, click the Edit button on the toolbar on the Use panel. That button opens RegexMagic’s source code template editor. It allows you to edit all the source code templates to your heart’s content, and even create your own.

If you really want to copy and paste just the regular expression into your source code, click the Copy button and select the string style for your programming language. This copies your regex quoted as a string or literal regex, with the appropriate characters escaped. Note that selecting a string style in the Copy menu does not automatically change the regex flavor. If the regex flavor is set to JavaScript and you click Copy as Python Script, you’ll get a regular expression using JavaScript’s regex syntax quoted as a Python raw string. While that may be useful if a Python server side script needs to pass a regex to a JavaScript client side script, make sure to select the proper regex flavor before copying your regex.