How to write a python regular expression to find any two letters that appears at least twice
How to write a python regular expression to find any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa) aaaa (aa) abcadb (ab), but not like aaa (aa, but it overlaps).
I already tried the following patterns none of them is capable of handle all the cases in the above statment.
r"(.)(?!\1)(.)\1" cannot pass "abdba" r"(.)(?<!\1)(.)\1" cannot pass "xyxy" r"(.)(?<!\1)(.)(?!\2)" cannot pass "xy开发者_如何学JAVAxy"
I use python's re package to run the above patterns.
We can use
x = input()
y = ""
s = 0
h = ""
for i in x:
if i in y:
s = s+1
y = y + i
if s == 1:
h = h+i
s = 0
y = y.replace(i, "")
print(h)
精彩评论