Regex character repeats n or more times in line with grep

蹲街弑〆低调 提交于 2019-12-19 08:59:16

问题


I need to find the regex expression to find a character that repeats 4 or more times with 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:

gexamplegofgvalidgmatchg

gothergvalidgmatchgisghereg

ggggother


回答1:


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

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

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

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

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

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


来源:https://stackoverflow.com/questions/47920855/regex-character-repeats-n-or-more-times-in-line-with-grep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!