正则表达式字符与grep一致重复n次或更多次

问题描述 投票:2回答:1

我需要找到正则表达式来找到一个用grep重复4次或更多次的字符。

我知道表达式是{n,},所以如果我需要找到行,例如,当字符“g”重复4次或更多次时,理论上用grep man page是:

grep "g{4,}" textsamplefile

但是不起作用。有帮助吗?

角色可以有其他字母。例如,有效匹配是:

gexamplegofgvalidgmatchg

gothergvalidgmatchgisghereg

ggggother

regex grep repeat
1个回答
1
投票

你应该改变你的grep命令:

grep -E 'g{4,}' input_file # --> this will extract only the lines containing chains of 4 or more g

如果你想获取包含4个或更多相同字符的链的所有行,你的正则表达式变为:

grep -E '(.)\1{3,}' input_file

如果您不需要链条但只需要g出现4次或更多次的行:

grep -E '([^g]*g){4}' input_file

你可以使用以下方法推广到任何重复4次或更多次的字符:

grep -E '(.)(.*\1){3}' input_file
© www.soinside.com 2019 - 2024. All rights reserved.