【发布时间】:2016-02-12 22:40:55
【问题描述】:
如何使用正则表达式查找至少有两次相同单词的行?
我试过了:
egrep '\w{2,}\1' file
但是终端给了我错误:
egrep: 无效的反向引用号
【问题讨论】:
-
检查我的编辑;应该这样做。
如何使用正则表达式查找至少有两次相同单词的行?
我试过了:
egrep '\w{2,}\1' file
但是终端给了我错误:
egrep: 无效的反向引用号
【问题讨论】:
您当前的正则表达式存在几个问题。
\b word boundaries 将单词限制在左侧和右侧。.* 以匹配any amount 之间的any characters。echo "ABC foo ABC bar" | egrep '\b(\w{2,})\b.*\b\1\b'
ABC foo ABC 条
echo "ABC foo ABCD bar" | egrep '\b(\w{2,})\b.*\b\1\b'
false
See demo at regex101。如果需要,请使用 egrep -o --only-matching 提取相关部分。
您可以进一步使用 .*? lazy dot 和 grep-P --perl-regexp 尽可能少的次数。
【讨论】:
试试这个:
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
【讨论】: