簡體   English   中英

從 Python 中的字符串中刪除重復的尾隨字符

[英]Removing repeated trailing characters from a string in Python

我有一個帶有評論的字段。 一些評論只是“否”,但尾隨有不同的“o”。 我想對這些評論進行轉換,以便只返回“否”。 如何使用正則表達式實現此目的?

例如:

remove_trailing_os("noooooo")應該是 output "no"

remove_trailing_os("nooOOoooooooo")應該是 output "no"

您可以使用不區分大小寫的反向引用:

import re
re.sub(r'(.)(?i:\1)+$', r'\1', "nooOOoooooooo", re.I)

output: 'no'

正則表達式:

(.)        # match a character
(?i:\1)+$  # match trailing case insensitive repeats of the character

您可以嘗試加入

cc = "noooooo"
cc1= "nooOOoooooooo"
print(''.join(sorted(set(cc), key=cc.index)))
print(''.join(sorted(set(cc1.lower()), key=cc1.index)))

會給

no
no

也可以使用正則表達式

repeat_pattern = re.compile(r'(\w)\1*', flags=re.IGNORECASE)
d = repeat_pattern.sub(r"\1", cc)
d1 = repeat_pattern.sub(r"\1", cc1)
print(d)
print(d1)

也會給

no
no

這似乎類似於如何在第二次出現“”(空格)后刪除所有字符

但本質上你想用o替換空間。 因此

## Assuming the two instances
t = 'noooooo'
t2 = 'nooOOoooooooo'
## Trying them on the two instances
t[:t.find('o',t.find('o')+1)]
t2[:t2.find('o',t2.find('o')+1)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM