Regex to match previous group in template?

I can never find regex documentation for matching capture groups as part of a pattern. For example:

(\w\d\w):$1 

.. must match a4b:a4b

$1 doesn't work, but I know something like that. Somebody knows?

+7
source share
1 answer

In the regular expression pattern, the back link to the first capture group is always \1 , not $1 .

Reason: $ means "end of line" (or end of line, depending on context) in the regular expression.

In a replacement pattern (which is not a regular expression), some dialects allow $1 (e.g. .NET, Java, Perl and JavaScript), some allow \1 (Python and Ruby), and some allow both (PHP and JGSoft).

Edit: since you wrote that you could not find the documentation on this, check out these reviews on the regular-expressions.info page:

+9
source

All Articles