2016-02-12 82 views

回答

1

試試這個:

egrep '(\w{2,}).*\1' file 

如果你沒有捕獲組( (...)),那麼沒有任何反向引用。

下面是一個例子:

$ cat file 
this line has the same word twice word 
this line does not 
this is this and that is that 

$ egrep '(\w{2,}).*\1' file 
this line has the same word twice word 
this is this and that is that 
+0

謝謝,但我認爲上面的答案更好地解決了這個問題,因爲它在雙方都添加了\ b字邊界。 – Amber

+0

我同意:)沒問題。 – Will

3

有與您當前的正則表達式的幾個問題。

  1. 使用捕捉字capturing groupbackreference它。
  2. 添加\bword boundaries用於將詞語限制在左側和右側。
  3. 添加.*匹配any amount之間的any characters之間。
echo "ABC foo ABC bar" | egrep '\b(\w{2,})\b.*\b\1\b' 

ABC foo ABC bar

echo "ABC foo ABCD bar" | egrep '\b(\w{2,})\b.*\b\1\b' 

false

See demo at regex101。如果需要,使用egrep -o- 僅匹配來提取相關部分。
您可以進一步使用.*?lazy點與grep-P--perl-regexp儘可能少的次數。

+0

謝謝你的幫助! – Amber

+0

@黃歡迎您! –

相關問題