noa*_*ale 12 python regex django non-ascii-characters hashtag
我目前使用re.findall查找并隔离字符串中哈希标记的'#'字符后的单词:
hashtags = re.findall(r'#([A-Za-z0-9_]+)', str1)
Run Code Online (Sandbox Code Playgroud)
它搜索str1并找到所有的hashtags.这可行,但它不考虑像这样的重音字符,例如:áéíóúñü¿
.
如果其中一个字母在str1中,它会将标签保存到它之前的字母.例如,#yogenfrüz
将是#yogenfr
.
我需要能够解释所有带有德语,荷兰语,法语和西班牙语的重音字母,以便我可以保存像 #yogenfrüz
我怎么能这样做呢
Ibr*_*jar 25
请尝试以下方法:
hashtags = re.findall(r'#(\w+)', str1, re.UNICODE)
Run Code Online (Sandbox Code Playgroud)
编辑 检查下面Martijn Pieters的有用评论.
zan*_*nga 19
我知道这个问题有点过时,但您也可以考虑将重音字符范围 \xc3\x80 (索引 192)和 \xc3\xbf (索引 255)添加到原始正则表达式中。
\nhashtags = re.findall(r'#([A-Za-z0-9_\xc3\x80-\xc3\xbf]+)', str1)\n
Run Code Online (Sandbox Code Playgroud)\n这将返回['#yogenfr\xc3\xbcz']
希望这对其他人有帮助。
\n您可能还想使用
\n\nimport unicodedata\noutput = unicodedata.normalize(\'NFD\', my_unicode).encode(\'ascii\', \'ignore\')\n
Run Code Online (Sandbox Code Playgroud)\n\n我如何将所有这些转义字符转换为各自的字符,例如如果存在 unicode \xc3\xa0,我如何将其转换为标准 a?\n假设您已将 unicode 加载到名为 my_unicode 的变量中...规范化 \ xc3\xa0 变成 a 就是这么简单...
\n\nimport unicodedata\noutput = unicodedata.normalize(\'NFD\', my_unicode).encode(\'ascii\', \'ignore\')\n显式示例...
\n\nmyfoo = u\'\xc3\xa0\xc3\xa0\'\nmyfoo\nu\'\\xe0\\xe0\'\nunicodedata.normalize(\'NFD\', myfoo).encode(\'ascii\', \'ignore\')\n\'aa\'\n
Run Code Online (Sandbox Code Playgroud)\n\n检查这个答案它对我帮助很大: How to conversion unicodeanetatedcharacterstopureasciiwithoutaccents?
\n