除去python中与关键字相关的提及

2024-04-19 01:15:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一条微博:

I playing with @mention1, @mention2 and @mention3 in the field

如何删除@提到1@提到2但保留@提到3,因为它与关键字相关?你知道吗

谢谢你。:-)


Tags: andtheinfieldwith关键字playingmention3
1条回答
网友
1楼 · 发布于 2024-04-19 01:15:29

沿着这些线的单行解决方法如何:

s = re.sub(r'KEEPME', '@mention3', re.sub(r'@[A-Za-z0-9]+', '', re.sub(r'@mention3', 'KEEPME', 'I playing with @mention1, @mention2 and @mention3 in the field')))

如果我能理解你的问题?你知道吗

我只需要确保使用一个唯一的字符串,它不会出现在真正的tweet中,而不是“KEEPME”。你知道吗

相关问题 更多 >