正则表达式字符根据grep重复n次或更多次 [英] Regex character repeats n or more times in line with grep

查看:1036
本文介绍了正则表达式字符根据grep重复n次或更多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to find the regex expression to find a character that repeats 4 or more times with grep.

我知道表达式为{n,},因此,例如,如果我需要查找行,则当字符"g"重复4次或更多次时,理论上grep手册页为:

I know that the expression is {n,}, so if I need to find lines, for example, when the character "g" repeats 4 or more times, in theory with grep man page is:

grep "g{4,}" textsamplefile

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

But doesn't work. Any help?

字符可能还有其他字母.例如,有效的匹配项是:

The character could have other letters. For example, a valid match is:

g 个示例 g g 有效 g 匹配 g

g 其他 g 有效 g 匹配 g g 此处 g

gothergvalidgmatchgisghereg

gggg 其他

推荐答案

您应该在以下位置更改grep命令:

you should change your grep command in:

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

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

if you want to take all the lines that contain chains of 4 or more identical characters your regex become:

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

如果不需要链条,而仅g出现4次或更多次的行:

If you do not need the chains but only line where g appear 4 or more times:

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

您可以使用以下方法概括为重复4次或更多次的任何字符:

you can generalize to any char repeating 4 times or more by using:

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

这篇关于正则表达式字符根据grep重复n次或更多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆