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

查看:65
本文介绍了正则表达式字符根据 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:

gexamplegofgvalidgma​​tchg

gexamplegofgvalidgmatchg

gotherg有效g匹配gg这里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天全站免登陆