This is what I tried:
(.)(?=.*\1)
This removes all instances of duplicates and leaves only the last instance, ie.
telnet -> lnet
I want this result:
telnet -> teln
How do I do this? I tried looking behind, but that only accepts a fixed length as far as I know.
Need to find a REGEX for this. I know other methods to achieve this without regex
Pure regex solution is not possible.You can try with callback function though.
z=[]
def fun(matchobj):
if matchobj.group(1) in z or matchobj.group(2) in z:
return ''
else:
if matchobj.group(1):
z.append(matchobj.group(1))
else:
z.append(matchobj.group(2))
return z[-1]
x="telnet"
print re.sub(r"(.)(?=.*\1)|(.)", fun, x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With