【问题标题】:Words with repeated letters in BashBash 中有重复字母的单词
【发布时间】:2014-11-21 18:56:28
【问题描述】:
egrep \e '(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z){2,}' dict.txt

我做错了什么?字典中的单词都是小写的。

如果egrep不支持间隔功能{},我应该使用哪个命令?

【问题讨论】:

  • 这是你想要的吗? ([a-z])\1
  • 我不知道您可以在同一个表达式中引用匹配项。成功了,非常感谢!
  • 是的,没问题!这里有更多关于backreferences的信息。
  • Linux 中的选项使用-,而不是反斜杠:egrep -e 代替egrep \e

标签: regex linux bash command-line


【解决方案1】:

你可以这样做,

egrep -e '([a-z])\1+' file

例子:

$ cat file
bbar 
fooohsg
jhfd
$ egrep -e '([a-z])\1+' file
bbar 
fooohsg

【讨论】:

    【解决方案2】:

    你需要转义{}

    grep '\([a-z]\)\1\{1,\}' dict.txt 将匹配任意数量的重复

    $ echo "aab" | grep '\([a-z]\)\1\{1,\}'
    aab
    

    【讨论】:

    • 谁说它会匹配重复的字符?运行这个命令echo "abc" | grep '[a-z]\{2,\}'
    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多