python - 如何从字符串python中删除所有表情符号(unicode)字符

标签 python regex string python-regex

我有以下字符串:

tweet = "Get $10 worth of AMAL!!\\nThis campaign will be final AirDrop before official release!!\\nhttps://form.run/@airdrop-e\xa0\\n\\nRT please!\\n\\n#amanpuri #AMAL\\n#BTC #XRP #ETH \\n#cryptocurrency  \\n#China #bitcoin \\n#\\xe3\\x82\\xa2\\xe3\\x83\\x9e\\xe3\\x83\\xb3\\xe3\\x83\\x97\\xe3\\x83\\xaa"

我需要清理它,但我坚持删除字符串末尾的符号,即 \\n#\\xe3\\x82\\xa2\\xe3最有可能是 unicode 符号、表情符号和换行符 \\n 这是我所做的:

pat1 = r'@[A-Za-z0-9]+' # this is to remove any text with @ (links)
pat2 = r'https?://[A-Za-z0-9./]+'  # this is to remove the urls
pat3 = r'[^a-zA-Z0-9$]' # to remove every other character except a-z & 0-9 & $
combined_pat2 = r'|'.join((r'|'.join((pat1, pat2)),pat3)) # combine pat1, pat2 and pat3 to pass it in the cleaning steps

我得到以下输出:

get $10 worth of amal   nthis campaign will be final airdrop before official release   n   e  n nrt please  n n amanpuri  amal n btc  xrp  eth  n cryptocurrency   n china  bitcoin  n  xe3 x82 xa2 xe3 x83 x9e xe3 x83 xb3 xe3 x83 x97 xe3 x83 xaa

所以我仍然拥有所有这些nxe3有人可以为此目的建议一个Python正则表达式吗?提前谢谢。

最佳答案

这些不是字符。他们是逃避者。您可以使用此正则表达式来匹配它们:

r'\\(n|x..)'

如果您想删除它们,请使用:

import re
tweet = re.sub(r'\\(n|x..)', '', tweet)

关于python - 如何从字符串python中删除所有表情符号(unicode)字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59167852/

相关文章:

python - 没有 '+' 运算符的字符串连接

python - 如何将复合词添加到 NLTK 中的标注器?

java - 如何在嵌套字符串中使用正则表达式进行替换?

regex - 在R中隔离字符串的一部分

regex - 使用 sed 在附加文本之前追加两个新行

javascript - array.map 在字符串中连接时自动附加逗号?

python - 是否有 `difflib.get_close_matches()` 的替代方法返回索引(列表位置)而不是 str 列表?

python - Python 打包的当前 future 是否发生了变化?

python - 为什么 python 的 `urllib.request` 中的网站响应与直接从网络浏览器发送的请求不同?

python - 混淆的 mandlebrot 函数 - 有人可以对其进行反混淆吗?