How to define loops in a regex pattern?

I want to state that a string contains two identical substrings.

I tried this regex: [A-ZA-Z]{2}

What I want to discover:

AZAZ
CUCU
PIPI

But this regex just checks to see if there are 4 in the top line, so

ABCD --> it good and it shouldn't be 

I am sorry if this is unclear, I do not understand how to write a regular expression to confirm repetitions.

+4
source share
1 answer

With Python, you can do the following:

p="((?P<two_chars>[A-Z]{2})(?P=two_chars))"
s="AZAZABCDCUCUPIPI"
re.findall(p, s)
[('AZAZ', 'AZ'), ('CUCU', 'CU'), ('PIPI', 'PI')]

and then extract the items you want from the list. Or you can be faster and do:

[k for k,v in re.findall(p,s)]
['AZAZ', 'CUCU', 'PIPI']

Hope this helps.

+2
source

All Articles