Copying and Pasting Regular Expressions

Copy and paste is the simple and easy way to transfer a regular expression between RegexMagic and your searching, editing and coding tools. Use the Copy button on the Regex panel to transfer the regular expression in different formats. If you often use RegexMagic in conjunction with a particular tool or application, you may want to see if it is possible to integrate RegexMagic with that tool.

The search boxes of text editors, grep utilities, etc. do not require the regular expression to be formatted in any way, beyond being a valid regex pattern. This is exactly the way the Regex panel in RegexMagic displays the generated regular expression. You can simply copy the regex “as is” if you want to paste it into the search box of an application.

If you want to use the regular expression in the source code of an application or script you are developing, the regex needs to be formatted according to the rules of your programming language. Some languages, such as Perl and JavaScript use a special syntax reserved for regular expressions. Other languages rely on external libraries for regular expression support, requiring you to pass regexes to their function calls as strings. This is where things get a bit messy.

Matching Modes, Flavors, and String Styles

Matching modes like “case insensitive” and “dot matches line breaks” are generally not included as part of the regular expression. That means they aren’t included when copying and pasting regular expressions. The only exceptions are the JavaScript, Perl, PHP, and Ruby operators. For all the other string styles, you’ll need to make sure by yourself that you’re using the same matching modes in your actual tool or source code as those indicated by RegexMagic on the Regex panel. If you want RegexMagic to take care of this for you, use the source code snippets on the Use tab rather than direct copy and paste.

The string style for copying the regex is independent of the regular expression flavor. Suppose you select Python in the drop-down list with applications, generate a regular expression, and then select the Copy as JavaScript String command. That will give you a regular expression using Python’s regex flavor formatted as a JavaScript string. This could be useful if your Python application reads its regular expressions from a JSON (JavaScript Object Notation) file. But if you wanted to use it in a JavaScript application, you need to select JavaScript in the drop-down list with applications before selecting Copy as JavaScript String. The string style that best matches your chosen application is always listed directly under the Copy button. All the other string styles can be found in the submenu.

Copying The Regex as a String or Operator

In regular expressions, metacharacters must be escaped with a backslash. In many programming languages, backslashes appearing in strings must be escaped with another backslash. This means that the regex \\ which matches a single backslash, becomes "\\\\" in Java or C/C++. The regex "\\" which matches a single backslash between double quotes, becomes "\"\\\\\"". How’s that for clarity?

When you generate code snippets on the Use tab, RegexMagic automatically inserts your regexes in the proper format into the code snippets, adapting them to the whims of each language. To use a regex you prepared in RegexMagic without creating a code snippet, click the Copy button on the toolbar on the Regex panel. You can copy the regular expression to the clipboard in one of several formats. Directly under the Copy button, you can find the string style used by the programming language you’ve selected on the Use panel. If the current regular expression flavor usually uses a different string style, that one will also be listed directly under the Copy button. All other string styles will be listed under the “Copy regex as” submenu.

As is: Copies the regex unchanged. Appropriate for tools and applications, but not for source code.

Basic-style string: The style used by programming languages derived from Basic, including Visual Basic and Xojo (REALbasic). A double-quoted string. A double quote in the regex becomes two double quotes in the string.

C string: A string of char in C and C++. A double-quoted string. Backslashes and double quotes are escaped with a backslash. Supports escapes such as \t, \n, \r, and \xFF at the string level.

C Wide string: A string of wchar_t in C and C++. A double-quoted string prefixed with the letter L. Backslashes and double quotes are escaped with a backslash. Supports escapes such as \t, \n, \r, \xFF, and \uFFFF at the string level.

C++11 Raw string: A string of char in C++11 quoted as a Raw string. Raw strings can contain literal line breaks and unescaped quotes and backslashes. Does not support any character escapes. If the regex contains the characters )" then RegexMagic automatically uses a longer custom delimiter to make sure the delimiter does not occur in the regex. If the regex does not contain any line breaks, quotes, or backslashes a normal double-quoted string is copied as then there is no benefit to using a raw string.

C++11 Wide Raw string: A string of wchar_t in C++11 quoted as a Raw string.

C# string: If the regex contains backslashes, it will be copied as a verbatim string for C# which doesn’t require backslashes to be escaped. Otherwise, it will be copied as a simple double-quoted string.

Delphi string: The style used by Delphi and other programming languages derived from Pascal. A single-quoted string. A single quote in the regex becomes two single quotes in the string.

Delphi Prism string: The style used by Delphi Prism, formerly known as Oxygene or Chrome. Either a single-quoted string on a single line, or a double-quoted string that can span multiple lines. A quote in the regex becomes two quotes in the string.

Groovy string: The Groovy programming language offers 5 string styles. Single-quoted and double-quoted strings require backslashes and quotes to be escaped. Using three single or three double quotes allows the string to span multiple lines. For literal regular expressions, the string can be delimited with two forward slashes, requiring only forward slashes to be escaped.

Java string: The style used by the Java programming language. A double-quoted string. Backslashes and double quotes are escaped with a backslash. Unicode escapes \uFFFF allowed.

JavaScript operator: A Perl-style // operator that creates a literal RegExp object in the ECMAScript programming language defined in the ECMA-262 standard, and its implementations like JavaScript, JScript and ActionScript. ECMA-262 uses mode modifiers that differ from Perl’s.

JavaScript string: The string style defined in the ECMA-262 standard and used by its implementations like JavaScript, JScript and ActionScript. A single-quoted or double-quoted string. Backslashes and quotes are escaped with a backslash. Unicode escapes \uFFFF and Latin-1 escapes \xFF allowed.

Perl-style string: The style used by scripting languages such as Perl and Ruby, where a double-quoted string is interpolated, but a single-quoted string is not. Quotes used to delimit the string, and backslashes, are escaped with a backslash.

Perl operator: A Perl m// operator for match and split actions, and an s/// operator for replace actions.

PHP string: A string for use with PHP’s ereg functions. Backslashes are only escaped when strictly necessary.

PHP ‘//’ preg string: A Perl-style // operator in a string for use with PHP’s preg functions.

PostgreSQL string: A string for PostgreSQL, delimited by double dollar characters.

PowerShell string: A string for PowerShell. Uses “here strings” for multi-line strings. Quotes and non-printables are escaped with backticks.

Python string: Unless the regex contains both single and double quote characters, the regex is copied as a Python “raw string”. Raw strings to not require backslashes to be escaped, making regular expressions easier to read. If the regex contains both single and double quotes, the regex is copied as a regular Python string, with quotes and backslashes escaped.

R string: The string style used by the R programming language. A single-quoted or double-quoted string. Backslashes and quotes are escaped with a backslash. Unicode escapes \U0010FFFF, basic Unicode escapes \uFFFF, and Latin-1 escapes \xFF allowed.

Ruby operator: A Perl-style // operator for use with Ruby. Ruby uses mode modifiers that differ from Perl’s.

Scala string: Copies the regex as a triple-quoted Scala string which avoids having to escape backslashes and allows line breaks which is handy for free-spacing regular expressions.

SQL string: The string style used by the SQL standard. A single-quoted string. A single quote in the regex becomes two single quotes in the string. The string can span multiple lines. Note that not all databases use this string style. E.g. MySQL uses C-style strings, and PostgreSQL uses either C-style strings or dollar-delimited strings.

Tcl word: Delimits the regular expression with curly braces for Tcl. In Tcl parlance, this is called a word.

XML: Replaces ampersands, angle brackets and quotes with XML entities like & suitable for pasting into XML files.