Reputation: 79
Regex newbie alert please be gentle. I am having strings like:
sent = 'The type of vehicle FRFR7800 is the fastest'
I want to remove the duplicate occurence of the substring 'FR'. So the string should be:
sent = 'The type of vehicle FR7800 is the fastest'.
I think I have spent more than 2 hours reading/trying the tutorial of re and the more pythonic way of groupby and I really can't figure it out. I also searched the similar questions and most of the results are covering the case when there are I repeated same characters e.g. having strings like 'dddddaaaaaggggg' etc. A couple of them helped but I ended up deleting the all occurences of 'FR'.
For example I tried:
sent = re.sub(r'FC{1}', '', sent)
sent = re.sub(r'FC|', '', sent)
These removed completely the occurence of 'FR'. When I changed it to:
sent = re.sub(r'FC{2}', '', sent)
Nothing happened, the string remained having the 'FR' occurence repeated.
Can somebody help me with or give me a hint ?
Upvotes: 2
Views: 802
Reputation: 6234
import re
sent = "The type of vehicle FRFR7800 is the fastest"
regex = r"(\w{2})\1"
print(re.sub(regex, r"\g<1>", sent))
Upvotes: 2