gpt4 book ai didi

python - 在python中的字符串中查找unicodes的所有匹配项

转载 作者:太空宇宙 更新时间:2023-11-04 05:19:11 25 4
gpt4 key购买 nike

import re

b="united thats weak. See ya 👋"
print b.decode('utf-8') #output: u'united thats weak. See ya \U0001f44b'

print re.findall(r'[\U0001f600-\U0001f650]',b.decode('utf-8'),flags=re.U) # output: [u'S']

如何获得输出 \U0001f44b。请帮忙

我需要处理的表情符号是“😀❤️😁😂😃😄😎😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝 😞😟😠😡😢😣😤😥😦😧😨 😩😪😫😬😭😮😯😰😱😲😳😴 😵😶😷😸😹😻😼😽😾😿 🙀🙁🙂🙃🙄🙅🙆🙇🙈🙉🙊🙋🙌🙍🙎🙏🚀🚁🚂🚃🚄🚅< em>🚆🚇🚈🚉🚊🚋🚌🚍🚎🚏🚐🚑🚒🚓🚔🚕🚖🚗🚘🚙🚚🚛🚜🚝🚞🚟🚠🚡🚢🚣🚤🚥🚦🚧🚨🚩🚪🚫🚬🚭🚮🚯🚰🚱 🚲🚳🚴🚵🚶🚷🚸🚹🚺🚻🚼 🚽🚾🚿🛀🛁🛂🛃🛄🛅🛋🛌🛍 🛎🛏🛐🛠🛡🛢🛣🛤🛥🛩🛫 🛬🛰🛳🤐🤑🤒🤓🤔🤕🤖🤗🤘🦀🦁🦂🦃🦄🧀"

最佳答案

搜索 unicode 范围与搜索任何类型的字符范围完全相同。但是,您需要正确表示字符串。这是一个工作示例:

#coding: utf-8
import re

b=u"united thats weak. See ya 😇 "
assert re.findall(u'[\U0001f600-\U0001f650]',b) == [u'😇']
assert re.findall(ur'[😀-🙏]',b) == [u'😇']

注意事项:

  • 您需要在程序的第一行或第二行使用 #coding: utf-8 或类似内容。
  • 在您的示例中,您使用的表情符号 U-1f44b 不在 U-1f600 到 U-1f650 的范围内。在我的示例中,我使用了一个。
  • 如果要使用\U 来包含unicode 字符,则不能使用原始字符串前缀(r'')。
  • 但是如果您使用字符本身(而不是 \U 转义),那么您可以使用原始字符串前缀。
  • 您需要确保模式和输入字符串都是 unicode 字符串。它们都不能是 UTF8 编码的字符串。
  • 但是您不需要 re.U 标志,除非您的模式包含 \s\w 或类似的。

关于python - 在python中的字符串中查找unicodes的所有匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40902662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com