【发布时间】:2020-11-18 20:08:44
【问题描述】:
只要有更多字母,我想从字符串中删除重复的字母。例如,考虑以下列表:
aaa --> it is untouched because all are the same letters
aa --> it is untouched because all are the same letters
a --> not touched, just one letter
broom --> brom
school --> schol
boo --> should be bo
gool --> gol
ooow --> should be ow
我使用以下正则表达式来消除重复项,如下所示:
(?<=[a-zA-Z])([a-zA-Z])\1+(?=[a-zA-Z])
但是,这在字符串boo 中失败,该字符串保留为原始boo,而不是删除双o。 oow 也会发生同样的情况,它不会简化为 ow。
你知道为什么boo 不被正则表达式占用吗?
【问题讨论】:
-
gogol、gogool、googol和googool的输出应该是什么? -
应该都是果戈理。重复项仅适用于一个字母
标签: python python-3.x regex regex-lookarounds