manpagez: man pages & more
info grep
Home | html | info | man

File: grep.info,  Node: Back-references and Subexpressions,  Next: Basic vs Extended,  Prev: Anchoring,  Up: Regular Expressions

3.5 Back-references and Subexpressions
======================================

The back-reference ‘\N’, where N is a single nonzero digit, matches the
substring previously matched by the Nth parenthesized subexpression of
the regular expression.  For example, ‘(a)\1’ matches ‘aa’.  If the
parenthesized subexpression does not participate in the match, the
back-reference makes the whole match fail; for example, ‘(a)*\1’ fails
to match ‘a’.  If the parenthesized subexpression matches more than one
substring, the back-reference refers to the last matched substring; for
example, ‘^(ab*)*\1$’ matches ‘ababbabb’ but not ‘ababbab’.  When
multiple regular expressions are given with ‘-e’ or from a file (‘-f
FILE’), back-references are local to each expression.

   *Note Known Bugs::, for some known problems with back-references.

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.