Search code examples
regexgreppattern-matchingoverlapping

How to pattern-match an overlapping set of characters? (using regex and grep)


say we have

1: ..FAF..FAF..
2: ..FAF.......
3: ..FAFAF.....

I need to pattern-match a triplet that occurs at least 2 times in a string, for example line 1&3 (the dots are placed to make it readable, they are actually characters [A-Z]).

I have only the solution for line 1

egrep '([A-Z]{3,}).*\1'

PS this is a simplified version of my text file

PPS it has to be with grep

UPDATE

I think I found the answer:

egrep '([A-Z]{3}).*\1|([A-Z])([A-Z])\1\2\1'

Solution

  • here is my trial pattern

    (\b[A-Z]([A-Z]{2,})\2\b|\b([A-Z]{3,})\b.*\3)  
    

    Demo