查找和替换表情符号

时间:2021-07-26 09:59:19

标签: python regex nlp

我想检测文本中的表情符号,一旦找到我想将它们添加到列表中并在文本中删除它们

这是函数:

# detect emojis
# Reference : https://gist.github.com/slowkow/7a7f61f495e3dbb7e3d767f97bd7304b
emoji_list = []
def remove_emoji(text):
    emoji_pattern = re.compile("["
                           u"\U0001F600-\U0001F64F"  # emoticons
                               u"\u1F31D" # full moon face smile
                               u"\u1F44D" # thumbs up
                               u"\u1F44E" # thumbs down
                               u"\u1F614" # pensive face
                               u"\u1F620" # angry face
                               u"\u1F60E" # :) face with sunglasses
                           "]+", flags=re.UNICODE)
    if re.search(emoji_pattern, text):
        emoji_list.append(re.search(emoji_pattern, text).group())
    return emoji_pattern.sub(r'', text)

但是当我用这段文字测试它时:

remove_emoji(" ? Omg ? another ? emoji ??")
print(emoji_list)

并非所有表情符号都被替换,也并非全部都添加到列表中。

结果如下:


'  Omg  another ? emoji '

['0', '?']

为什么没有给出正确的结果?

0 个答案:

没有答案
相关问题