Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regular expression removing duplicate consecutive substrings in a string

I tried to do a search on this particular problem, but all I get is either removal of duplicate lines or removal of repeated strings where they are separated by a delimiter.

My problem is slightly different. I have a string such as

    "comp name1 comp name2 comp name2 comp name3" 

where I want to remove the repeated comp name2 and return only

    "comp name1 comp name2 comp name3" 

They are not consecutive duplicate words, but consecutive duplicate substrings. Is there a way to solve this using regular expressions?

like image 343
Rasika Avatar asked Dec 09 '22 09:12

Rasika


1 Answers

s/(.*)\1/$1/g

Be warned that the running time of this regular expression is quadratic in the length of the string.

like image 102
btilly Avatar answered Jan 22 '23 15:01

btilly