如何将表情符号添加到 Python 中的字母数字 RegEx

How to add emojis to alphanumerics RegEx in Python

如何将所有表情符号添加到 RegEx 中的字母数字,就像这样

pattern = r'\w+'

您可以使用 emoji 包 (pip install emoji) 获取表情符号集,并以这种方式将其与 \w+ 组合:(?:\w|<emoji_pattern>)+:

from emoji import UNICODE_EMOJI
import re

e_list = UNICODE_EMOJI.keys()
word_emoji_rx = re.compile(r"(?:\w|{})+".format("|".join(map(re.escape, sorted(e_list,key=len,reverse=True)))))
print(word_emoji_rx.findall(r'abc def ghi'))
# => ['abc', 'def', 'ghi']

看到一个Python demo