2012-04-14 13 views
6

Ho il seguente incarico:Cosa significa " 2" alla fine di un'espressione regolare

Le parole di una canzone si trovano in un file chiamato stairway.txt. Quale delle seguenti linee verrà stampata dopo questo comando:

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. 

Non capisco che cosa fa il \2 alla fine significa?

+0

Avere lo provate? Sono questi compiti? – Toto

+0

scala negata :) – Robbie

risposta

11

È un backreference.

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

backreference è un'espressione \ n dove n è un numero. Corrisponde al contenuto del n'insieme di parentesi nell'espressione.

Inoltre, la risposta è (d):

$ grep -E '(^.{4})(.{2}).*[ ]\2' test.txt 
If there's a bustle in your hedgerow. 
Problemi correlati