【问题标题】:How to remove all emoji (unicode) characters from a string python如何从字符串python中删除所有表情符号(unicode)字符
【发布时间】:2020-03-28 18:34:13
【问题描述】:

我有以下字符串:

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

所以我仍然有所有这些 n's 和 xe3's 有人可以为此目的建议一个 python 正则表达式吗?提前谢谢。

【问题讨论】:

    标签: python regex string python-regex


    【解决方案1】:

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

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

    如果要删除它们,请使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 2017-08-23
      相关资源
      最近更新 更多