python上的简单正则表达式

2024-09-26 22:09:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python2.X,从一个文件中加载一个文本,这是一行文本

Odio ¿Mañana pensar porque RT luego pasa lo que pasa Marzo ♡♡♡

我发现这个正则表达式[^\x30-\xFF]与我想要的字符完全匹配(使用在线regex工具),但是当我像这样在代码中使用它时:

filtered_comments = re.sub("[^\x30-\xFF]", " ", all_comments)

它不匹配相同的字符;您可以在http://pythex.org上尝试 所以在这篇文章中,我要匹配,然后留下niand .... 有什么想法吗?在


Tags: 文件文本字符commentsrtxffpython2ma
2条回答

all_commentstypestr还是{}?如果它是unicode类型并且字符打印正确,那么正则表达式应该可以工作。在

如果字符串是str类型,则需要使用正确的编码对其进行编码。假设您的编码是UTF-8,这将起作用:

filtered_comments = re.sub("[^\x30-\xFF]", " ", all_comments.decode('utf-8'))

另一件要注意的事情是:您^\x30-\xFF匹配!和{}以及{}以下的许多其他符号。也许你想要^\x20-\xFF,因为\x20是空间,它几乎是最低的典型ASCII字符?在

尝试下面的脚本,请在第一行看到#coding=utf-8。有关详细信息,请参见PEP-0263

# coding=utf-8
import re

comments = u"Odio ¿Mañana pensar porque RT luego pasa lo que pasa Marzo ♡♡♡"

rx = re.compile(u"[\u2661]+")

# If you want to remove non-ASCII characters, as you mentioned in comments,
# uncomment following regex. 
# Downside is it will remove all accented characters too.
#
# rx = re.compile(u"[^\x00-\x7F]+")

filtered_comments = re.sub(rx, " ", comments)

print filtered_comments

它会打印出来的

^{pr2}$

相关问题 更多 >

    热门问题