I can never seem to find any documentation on the regex for matching a capture group as part of the pattern. For example:
(\w\d\w):$1
..should match a4b:a4b
$1
doesn't work, but I know it's something similar. Anyone know?
Regular expressions allow us to not just match text but also to extract information for further processing. This is done by defining groups of characters and capturing them using the special parentheses ( and ) metacharacters. Any subpattern inside a pair of parentheses will be captured as a group.
Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .
A backreference in a regular expression identifies a previously matched group and looks for exactly the same text again. A simple example of the use of backreferences is when you wish to look for adjacent, repeated words in some text. The first part of the match could use a pattern that extracts a single word.
Advertisements. Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
In a regex pattern, a backreference to the first capturing group is always \1
, not $1
.
Reason: $
means "end of string" (or end of line, depending on context) in a regex.
In a replace pattern (which isn't a regex), 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 couldn't find any documentation on this, check out these overviews on regular-expressions.info:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With