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

5
  • 1
    "Need to find a REGEX for this." Really? Why? Commented Dec 10, 2016 at 4:26
  • I just want to know if it's possible. I think it's possible in .NET, but in python, could not think of a regex. The straightforward approach is simple enough, this is out of curiosity. Commented Dec 10, 2016 at 4:29
  • @TigerhawkT3 Would you be so kind as to unmark my question as a duplicate, or even better, link it to a more appropriate duplicate if it exists. Commented Dec 10, 2016 at 4:41
  • According to this, there's no single regex operation to do this. Commented Dec 10, 2016 at 4:51
  • 1
    It is possible with PyPi regex module. Commented Dec 10, 2016 at 10:09

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.