Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicate instances of a character using a regex expression using python

Tags:

python

regex

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

like image 450
NobleSiks Avatar asked Dec 12 '25 14:12

NobleSiks


1 Answers

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)
like image 92
vks Avatar answered Dec 15 '25 05:12

vks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!