Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "\2" mean at the end of a regular expression

Tags:

regex

linux

bash

I have the following assignment:

Words of a song are in a file called stairway.txt. Which of the following lines will be printed out after this command:

grep -E '(^.{4})(.{2}).*[ ]\2' stairway.txt

(a) Yes, there are two paths you can go by but in the long run

(b) Its just a spring clean for the May queen.

(c) Don't be alarmed now.

(d) If there's a bustle in your hedgerow.

(e) Theres still time to change the road you're on.

I don't understand what does the \2 at the end mean?

like image 411
Zippie Avatar asked Apr 14 '12 09:04

Zippie


1 Answers

It is a backreference.

From http://www.selectorweb.com/grep_tutorial.html:

Backreference is an expression \n where n is a number. It matches the contents of the n'th set of parentheses in the expression.

Also, the answer is (d):

$ grep -E '(^.{4})(.{2}).*[ ]\2' test.txt
If there's a bustle in your hedgerow.
like image 90
Delan Azabani Avatar answered Sep 28 '22 17:09

Delan Azabani