如何使用正則表達式來查找至少有兩次相同單詞的行?egrep找到一行至少有兩次相同的單詞
我想:
egrep '\w{2,}\1' file
但終端給我的錯誤:
egrep: invalid backreference number
如何使用正則表達式來查找至少有兩次相同單詞的行?egrep找到一行至少有兩次相同的單詞
我想:
egrep '\w{2,}\1' file
但終端給我的錯誤:
egrep: invalid backreference number
試試這個:
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
有與您當前的正則表達式的幾個問題。
\b
word boundaries用於將詞語限制在左側和右側。.*
匹配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儘可能少的次數。
謝謝你的幫助! – Amber
@黃歡迎您! –
檢查我的編輯;應該這樣做。 – Will