3

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

NobleSiks
  • 329
  • 2
  • 4
  • 17

2 Answers2

1

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)
vks
  • 67,027
  • 10
  • 91
  • 124
1

a little 'hack' would be ... to reverse the string before and after the lookahead

import re

expr = r'telnetrer'[::-1]
pr = re.sub(r'(.)(?=.*\1)', r'', expr)[::-1]

print(pr)

Output

>>> telnr
jwdasdk
  • 183
  • 7