Your regular expression, \(.\)\1
, will match any line with some character immediately followed by the same character. The .
matches any single character, the \(
and \)
surrounding the dot "captures" the matched substring, and the \1
is a back-reference referring back to the first capture group, i.e. the character just previously matched.
In your example input, you have a single line matching this expression:
88 9999 88
Here, the expression would match the initial 88
substring and grep
would therefore output the line to its standard output stream.
Note that your statement "backslash followed by a character is a regular expression" is a bit misleading. The whole expression is a regular expression (in this case, a "basic" regular expression as opposed to an "extended" regular expression), and the backslashes modify some characters' meanings. Had you used \[
, for example, the backslash would have removed the special meaning of [
(which introduces a bracketed expression, matching a single character from a set) and would instead force a match of a literal left bracket.
A string is not a regular expression because it contains a backslash. It's a regular expression because you use it with a utility that interprets it as a regular expression. Even a string such as hello
could be used as a regular expression (as such, it matches any string containing hello
as a substring).
88 9999 88
. Are you sure22
is returned? Or actually yourexemple
file has that content?