
| Easily work with any of these regex flavors today. Compose and analyze regex patterns with RegexBuddy's easy-to-grasp regex blocks and intuitive regex tree, which automatically adjusts itself to the particularities of the regex flavor you're working with. Safely test your regexes in RegexBuddy's sandbox, and get the same results your actual application would produce. Instantly convert a regular expression written for another regular expression flavor to the one you're using now. Let RegexBuddy take care of all the little details explained in the big comparison table below. Get your own copy of RegexBuddy now. |
The table below compares the various tokens that the various tools and languages discussed on this website recognize in the replacement text during search-and-replace operations.
The list of replacement text flavors is not the same as the list of regular expression flavors in the regex features comparison. The reason is that the replacements are not made by the regular expression engine, but by the tool or programming library providing the search-and-replace capability. The result is that tools or languages using the same regex engine may behave differently when it comes to making replacements. E.g. The PCRE library does not provide a search-and-replace function. All tools and languages implementing PCRE use their own search-and-replace feature, which may result in differences in the replacement text syntax. So these are listed separately.
To make the table easier to read, I did group tools and languages that use the exact same replacement text syntax. The labels for the replacement text flavors are only relevant in the table below. E.g. the .NET framework does have built-in search-and-replace function in its Regex class, which is used by all tools and languages based on the .NET framework. So these are listed together under ".NET".
Note that the escape rules below only refer to the replacement text syntax. If you type the replacement text in an input box in the application you're using, or if you retrieve the replacement text from user input in the software you're developing, these are the only escape rules that apply. If you pass the replacement text as a literal string in programming language source code, you'll need to apply the language's string escape rules on top of the replacement text escape rules. E.g. for languages that require backslashes in string literals to be escaped, you'll need to use "\\1" instead of "\1" to get the first backreference.
A flavor can have four levels of support (or non-support) for a particular token. A "YES" in the table below indicates the token will be substituted. A "no" indicates the token will remain in the replacement as literal text. Note that languages that use variable interpolation in strings may still replace tokens indicated as unsupported below, if the syntax of the token corresponds with the variable interpolation syntax. E.g. in Perl, $0 is replaced with the name of the script. The "string" label indicates that the syntax is supported by string literals in the language's source code. For languages like PHP that have interpolated (double quotes) and non-interpolated (single quotes) variants, you'll need to use the interpolated string style. String-level support also means that the character escape won't be interpreted for replacement text typed in by the user or read from a file. Finally, "error" indicates the token will result in an error condition or exception, preventing any replacements being made at all.
| Easily work with any of these regex flavors today. Compose and analyze regex patterns with RegexBuddy's easy-to-grasp regex blocks and intuitive regex tree, which automatically adjusts itself to the particularities of the regex flavor you're working with. Safely test your regexes in RegexBuddy's sandbox, and get the same results your actual application would produce. Instantly convert a regular expression written for another regular expression flavor to the one you're using now. Let RegexBuddy take care of all the little details explained in the big comparison table below. Get your own copy of RegexBuddy now. |
| Syntax Using Backslashes | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Feature | JGsoft | .NET | Java | Perl | ECMA | Python | Ruby | Tcl | PHP ereg | PHP preg | REAL basic | Oracle | Post gres | XPath |
| \& (whole regex match) | YES | no | no | no | no | no | YES | no | no | no | no | no | YES | error |
| \0 (whole regex match) | YES | no | no | no | no | YES | YES | YES | YES | YES | YES | no | no | error |
| \1 through \9 (backreference) | YES | no | no | depr. | no | YES | YES | YES | YES | YES | YES | YES | YES | error |
| \10 through \99 (backreference) | YES | no | no | no | no | YES | no | no | no | YES | YES | no | no | error |
| \10 through \99 treated as \1 through \9 (and a literal digit) if fewer than 10 groups | YES | n/a | n/a | n/a | n/a | no | n/a | n/a | n/a | no | no | n/a | n/a | error |
| \g<group> (named backreference) | YES | no | no | no | no | YES | no | no | no | no | no | no | no | error |
| \` (backtick; subject text to the left of the match) | YES | no | no | no | no | no | YES | no | no | no | no | no | no | error |
| \' (straight quote; subject text to the right of the match) | YES | no | no | no | no | no | YES | no | no | no | no | no | no | error |
| \+ (highest-numbered participating group) | YES | no | no | no | no | no | YES | no | no | no | no | no | no | error |
| Backslash escapes one backslash and/or dollar | YES | no | YES | YES | no | YES | YES | YES | YES | YES | YES | YES | YES | YES |
| Unescaped backslash as literal text | YES | YES | no | YES | YES | YES | YES | YES | YES | YES | no | YES | YES | error |
| Character Escapes | ||||||||||||||
| Feature | JGsoft | .NET | Java | Perl | ECMA | Python | Ruby | Tcl | PHP ereg | PHP preg | REAL basic | Oracle | Post gres | XPath |
| \u0000 through \uFFFF (Unicode character) | YES | no | string | no | string | u string |
no | string | no | no | no | no | no | error |
| \x{0} through \x{FFFF} (Unicode character) | YES | no | no | string | no | no | no | no | no | no | no | no | no | error |
| \x00 through \xFF (ASCII character) | YES | no | no | string | string | non-r string |
no | string | string | string | YES | no | no | error |
| Syntax Using Dollar Signs | ||||||||||||||
| Feature | JGsoft | .NET | Java | Perl | ECMA | Python | Ruby | Tcl | PHP ereg | PHP preg | REAL basic | Oracle | Post gres | XPath |
| $& (whole regex match) | YES | YES | error | YES | YES | no | no | no | no | no | YES | no | no | error |
| $0 (whole regex match) | YES | YES | YES | no | no | no | no | no | no | YES | YES | no | no | YES |
| $1 through $9 (backreference) | YES | YES | YES | YES | YES | no | no | no | no | YES | YES | no | no | YES |
| $10 through $99 (backreference) | YES | YES | YES | YES | YES | no | no | no | no | YES | YES | no | no | YES |
| $10 through $99 treated as $1 through $9 (and a literal digit) if fewer than 10 groups | YES | no | YES | no | YES | n/a | n/a | n/a | n/a | no | no | n/a | n/a | YES |
| ${1} through ${99} (backreference) | YES | YES | error | YES | no | no | no | no | no | YES | no | no | no | error |
| ${group} (named backreference) | YES | YES | error | no | no | no | no | no | no | no | no | no | no | error |
| $` (backtick; subject text to the left of the match) | YES | YES | error | YES | YES | no | no | no | no | no | YES | no | no | error |
| $' (straight quote; subject text to the right of the match) | YES | YES | error | YES | YES | no | no | no | no | no | YES | no | no | error |
| $_ (entire subject string) | YES | YES | error | YES | YES | no | no | no | no | no | no | no | no | error |
| $+ (highest-numbered participating group) | YES | no | error | YES | no | no | no | no | no | no | no | no | no | error |
| $+ (highest-numbered group in the regex) | no | YES | error | no | YES | no | no | no | no | no | no | no | no | error |
| $$ (escape dollar with another dollar) | YES | YES | error | no | YES | no | no | no | no | no | no | no | no | error |
| $ (unescaped dollar as literal text) | YES | YES | error | no | YES | YES | YES | YES | YES | YES | buggy | YES | YES | error |
| Tokens Without a Backslash or Dollar | ||||||||||||||
| Feature | JGsoft | .NET | Java | Perl | ECMA | Python | Ruby | Tcl | PHP ereg | PHP preg | REAL basic | Oracle | Post gres | XPath |
| & (whole regex match) | no | no | no | no | no | no | no | YES | no | no | no | no | no | no |
| General Replacement Text Behavior | ||||||||||||||
| Feature | JGsoft | .NET | Java | Perl | ECMA | Python | Ruby | Tcl | PHP ereg | PHP preg | REAL basic | Oracle | Post gres | XPath |
| Backreferences to non-existent groups are silently removed | YES | no | error | YES | no | error | YES | YES | no | YES | YES | YES | YES | YES |
The $+ token is listed twice, because it doesn't have the same meaning in the languages that support it. It was introduced in Perl, where the $+ variable holds the text matched by the highest-numbered capturing group that actually participated in the match. In several languages and libraries that intended to copy this feature, such as .NET and JavaScript, $+ is replaced with the highest-numbered capturing group, whether it participated in the match or not.
E.g. in the regex a(\d)|x(\w) the highest-numbered capturing group is the second one. When this regex matches a4, the first capturing group matches 4, while the second group doesn't participate in the match attempt at all. In Perl, $+ will hold the 4 matched by the first capturing group, which is the highest-numbered group that actually participated in the match. In .NET or JavaScript, $+ will be substituted with nothing, since the highest-numbered group in the regex didn't capture anything. When the same regex matches xy, Perl, .NET and JavaScript will all store y in $+.
Also note that .NET numbers named capturing groups after all non-named groups. This means that in .NET, $+ will always be substituted with the text matched by the last named group in the regex, whether it is followed by non-named groups or not, and whether it actually participated in the match or not.
Did this website just save you a trip to the bookstore? Please make a donation to support this site, and you'll get a lifetime of advertisement-free access to this site!
Page URL: http://www.Regular-Expressions.info/refreplace.html
Page last updated: 30 March 2008
Site last updated: 06 June 2008
Copyright © 2003-2008 Jan Goyvaerts. All rights reserved.
| Books |
| Teach Yourself Reg. Expr. |
| Mastering Regular Expr. |
| Java Regular Expressions |
| Oracle Regular Expr. |
| Regular Expr. Pocket Ref. |
| Regular Expr. Recipes |
| Regex Recipes for Windows |
| Regex Reference |
| Basic Regex Syntax |
| Advanced Regex Syntax |
| Unicode-Specific Syntax |
| Flavor-Specific Syntax |
| Flavor Comparison |
| Replacement Syntax |
| More Information |
| Introduction |
| Quick Start |
| Tutorial |
| Tools and Languages |
| Examples |
| Books |
| Reference |
| Print PDF |
| About This Site |
| RSS Feed |
| PowerGREP 3 |
| Use regular expressions to search through large numbers of text and binary files, such as source code, correspondence, server or system logs, reference texts, archives, etc. Quickly find the files you are looking for, or extract the information you need. Look through just a handful of files, or thousands of files and folders. |
| Perform comprehensive text and binary replacement operations for easy maintenance of websites, source code, reports, etc. Preview replacements before modifying files, and stay safe with flexible backup and undo options. |
| Work with plain text files, Unicode files, binary files, files stored in zip archives, and even MS Word documents, Excel spreadsheets and PDF files. Runs on Windows 98, ME, NT4, 2000, XP & Vista. |
| More information |
| Download PowerGREP now |