Copying and Pasting Regular Expressions

Copy and paste is the simple and easy way to transfer a regular expression between RegexBuddy and your searching, editing, and coding tools. You can use the regular Ctrl+C and Ctrl+V shortcut keys to copy and paste the selected part of the regex as is. Or, you can use the Copy and Paste buttons on RegexBuddy’s toolbar to transfer the regular expression in different formats. If you often use RegexBuddy in conjunction with a particular tool or application, you may want to see if it is possible to integrate RegexBuddy 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 you enter the regular expression in RegexBuddy, whether you type it in or insert tokens on the Create panel. So you can simply copy and paste the regex “as is”.

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.

Flavors and Matching Modes

Matching modes like “dot matches line breaks” and “case insensitive” 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 preg, 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 in RegexBuddy. If you want RegexBuddy to take care of this for you, use the source code snippets on the Use panel rather than direct copy and paste.

In particular, free-spacing mode is something you need to pay attention to. Many string styles do not support multi-line strings. When you tell RegexBuddy to copy a regular expression to the clipboard using one of these string styles, RegexBuddy copies a concatenation of multiple strings to the clipboard, one for each line in your regular expression. Each string will end with a line break, so comments in the regex are terminated correctly. That way your regex will be formatted the same way in your source code as in RegexBuddy.

When pasting the regular expression back into RegexBuddy, RegexBuddy cannot determine from the string alone whether the line breaks in the string are line breaks that your regular expression should match, or whether the line breaks are free-spacing line breaks. To solve this, RegexBuddy simply checks whether you’ve selected free-spacing or exact spacing in the drop-down list on the main toolbar. If you selected “exact spacing” then line breaks in the string are included as \r\n in the regex. If you selected “free-spacing” then line breaks are treated as whitespace that split the regex into multiple lines in RegexBuddy. To make sure RegexBuddy pastes your regular expression correctly, set the free-spacing mode in RegexBuddy to match the free-spacing option in your source code before you paste the regex.

Copying a regular expression doesn’t convert it into the correct regular expression flavor. For example, if you’re editing a regular expression with the PCRE flavor selected and you select the Copy as JavaScript Operator command, you’ll get a PCRE-style regular expression formatted as a JavaScript operator. If you want the regular expression to be fully converted to JavaScript, first convert the regular expression on the Convert panel. After clicking Accept Conversion, you can then select Copy as JavaScript Operator to copy the converted regex as a JavaScript operator.

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 panel, RegexBuddy automatically inserts your regexes correctly formatted as a strings or operators as required by the programming language. To use a regex you prepared in RegexBuddy without creating a code snippet, click the Copy button on the main toolbar. 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 in the list of applications. If the source code template for that application 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. Literal tabs and line breaks are represented by appending special constants such as vbTab and vbCrLf.

Basic-style multi-line string: Visual Basic 14 (Visual Studio 2015) and later allow literal tabs and line breaks in double-quoted strings.

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 RegexBuddy 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 then 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++11 UTF-8 string: A string of char in C++11 or char8_t in C++20. A double-quoted string prefixed with u8. Quoted as a Raw string if the regex contains literal line breaks, backslashes, or double quotes.

C++11 UTF-16 string: A string of char16_t in C++11. A double-quoted string prefixed with u. Quoted as a Raw string if the string contains literal line breaks, backslashes, or double quotes.

C++11 UTF-32 string: A string of char32_t in C++11. A double-quoted string prefixed with U. Quoted as a Raw string if the string contains literal line breaks, backslashes, or double quotes.

C# or F# string: If the regex contains backslashes then 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. C# strings copied by RegexBuddy can also be used with F#.

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. Literal line breaks and tabs are appended as character literals such as #9 and #13#10.

Delphi multi-line string: Delphi 12 and later support multi-line strings that are delimited by 3 or more single quotes on separate lines. RegexBuddy only uses this syntax if the regex contains literal line breaks. Otherwise it copies a regular Delphi 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. Slashy strings are 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 are \uFFFF allowed.

Java text block: Java 15 and later support text blocks that are delimited by 3 double quotes and which can contain literal line breaks. RegexBuddy only uses this syntax if the regex contains literal line breaks. Otherwise it copies a regular Java string.

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.

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.

MySQL string: A double-quoted string that requires backslashes and double quotes to be escaped with a backslash and supports character escapes such as \t, \n, and \r. This makes MySQL strings more like C strings rather than like standard SQL strings.

Perl-style string: The style used by Perl, 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 PHP string for use with PHP’s preg functions.

PostgreSQL string: A string for PostgreSQL, delimited by double dollar characters to avoid having to escape backslashes if the regex does not contain literal line breaks. Otherwise as a single-quoted string with an E prefix so that character escapes can be used to represent literal line breaks.

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 if it contains line breaks or backslashes, which avoids having to escape backslashes and allows line breaks which is handy for free-spacing regular expressions. Otherwise it copies a regular Java string, which Scala also supports.

Spreadsheet formula: Copies the regex as double-quoted string that can be used spreadsheet formulas in LibreOffice Calc and Google Sheets. Literal line breaks and tabs are appended using the & operator and the CHAR() function.

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. RegexBuddy has separate string styles for PostgreSQL and MySQL.

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.

Pasting a String or Operator as The Regular Expression

RegexBuddy can do the opposite conversion when you want to edit a regular expression that is already part of an application’s source code. In your source code editor or IDE, select the entire string or operator that holds the regular expression. Make sure quotes and other delimiters are included. Copy it to the clipboard.

Then switch to RegexBuddy. Make sure to first set the “free-spacing” or “exact spacing” option to make sure that regexes copied using string styles that don’t support literal line breaks are pasted correctly. Then click the Paste button on RegexBuddy’s main toolbar. The Paste button’s menu offers similar options as the Copy menu, except that they work the other way around. If the clipboard holds the Java string "\"\\\\\"", select “Regex form Java string” and RegexBuddy will properly interpret the string as the regular expression "\\", which matches a single backslash between a pair of double quote characters. When you’re done editing the regex, select “Copy as Java string” and you can paste the updated regex as a Java string into your source code.

The Paste menu has fewer items than the Copy menu because some of the Paste commands can handle multiple string styles can can be distinguished by prefixes or quotes.

Basic-style string: Can paste a regex copied as a Basic-style string or as a Basic-style multi-line string.

C string: Can paste a regex copied as any of the C string styles. Wide, raw, UTF-8, UTF-16, and UTF-32 prefixes are all interpreted.

Delphi string: Can paste a regex copied as a Delphi string or as a Delphi multi-line string.

Java string or text block: Can paste a regex copied as a Java string or as a Java text block.

The Paste menu does have separate items for pasting a C# string and pasting an F# string. Though C# and F# both use double-quoted string and an @ prefix for vertabim strings, the character escapes they allow within non-verbatim strings are different. When copying this does not matter because RegexBuddy does not generate these escapes. But when pasting they do matter because RegexBuddy needs to be able to correctly paste any C# or F# string, not just those it produces itself.

Just like when copying regular expressions, you need to take care that you’ve set the same matching modes in RegexBuddy as in the application or source code you’ve copied the regex from. Pasting a regular expression also doesn’t change the programming language or application selected in RegexBuddy, nor does it convert the regular expression to the selected application. Make sure to select the correct programming language or application before pasting the regular expression.

Copying and Pasting Replacement Strings

The Copy and Paste buttons have a second set of items for copying and pasting the replacement text formatted as a string literal. The available string styles are largely the same. JavaScript operator and PHP '//' preg string are not available because they can’t be used for replacement text. Use JavaScript string or PHP string instead. Perl operator is not available because the Copy Regex as Perl Operator command already includes the replacement text in the s/// operator when RegexBuddy is in Replace mode. Ruby string replaces the Ruby operator format because Ruby uses its normal string syntax for the replacement text, but a special // syntax for regular expressions.