Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for matching a previous group in the pattern?

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?

like image 663
mowwwalker Avatar asked Jun 24 '12 19:06

mowwwalker


People also ask

How do I match a group in regex?

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.

How do I match a pattern in regex?

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 "@" .

What is a back reference regex?

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.

What is regex capturing group?

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".


1 Answers

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:

  • Grouping and Backreferences
  • Regex Replacement Syntax
like image 174
Tim Pietzcker Avatar answered Sep 24 '22 12:09

Tim Pietzcker