Perl正则表达式删除字符串中重复的连续子字符串 [英] Perl regular expression removing duplicate consecutive substrings in a string
本文介绍了Perl正则表达式删除字符串中重复的连续子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图对这个特定问题进行搜索,但我得到的只是删除重复行或删除以分隔符分隔的重复字符串.
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"
我想删除重复的comp name2并只返回
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?
推荐答案
s/(.*)\1/$1/g
请注意,此正则表达式的运行时间是字符串长度的二次方.
Be warned that the running time of this regular expression is quadratic in the length of the string.
这篇关于Perl正则表达式删除字符串中重复的连续子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文