Search code examples
regexregex-lookaroundslookbehind

Regex - Match redundant sequences


I would like to design a regexp that would capture all sequences of letters that are contained at least twice in a larger string :

For instance, let's take :

abzabuiabuz => a, b, ab, z, bu, u

I was thinking of lookaheads and lookbehinds but I can't see a proper solution.

Many thanks for your help!

EDIT:

My original issue is to find -a, -b in eu-a us-b eu-c eu-b us-a us-a.


Solution

  • Capture one or more characters and make a back-reference to those captured characters and then put the whole pattern inside a positive lookahead assertion.

    (?=(.+).*\1)
    

    DEMO

    Get the duplicated chars from the group index 1.