1

From what I understood, backslash followed by a character is a regular expression.

But I can't figure out what this command does.

$ grep '\(.\)\1' < exemple
22
88 9999 88
$ cat exemple
1 1 1 1 1
abc bc fghi
123 45 678
ABC BC FGHI
88 9999 88

The dot is supposed to match any character, and the parenthesis match a pattern (not sure about this one) and I don't understand what the \1 does.

6
  • 1
    Related: Using grep to find multiple repeating characters in a word Commented Nov 20, 2022 at 23:33
  • With your command I only get this line: 88 9999 88. Are you sure 22 is returned? Or actually your exemple file has that content? Commented Nov 21, 2022 at 3:37
  • @EdgarMagallon They are not showing the correct data in the question.
    – Kusalananda
    Commented Nov 21, 2022 at 9:19
  • @Kusalananda I supposed that (but I was not really sure). And if I'm not wrong their grep command will match any character followed by the same one Commented Nov 21, 2022 at 9:44
  • 1
    @EdgarMagallon That is what the given expression does, yes.
    – Kusalananda
    Commented Nov 21, 2022 at 9:50

2 Answers 2

4

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

0

\1 looks to be a back-reference to the capture group (the part surrounded by parenthesis). This is useful in situations where you might want to only output a specific part of a match. I used these a while back: https://stackoverflow.com/questions/65641590/sed-expression-just-prints-entire-file

1
  • 1
    You mean using \1 in the replacement part of the s command in an sed script? For basic regular expressions, you can also use backreferences inside the pattern to match for repeating substrings.
    – Philippos
    Commented Nov 21, 2022 at 6:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .