Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式-删除除字母数字和重音符号以外的所有特殊字符_Python_Regex - Fatal编程技术网

Python 正则表达式-删除除字母数字和重音符号以外的所有特殊字符

Python 正则表达式-删除除字母数字和重音符号以外的所有特殊字符,python,regex,Python,Regex,如何删除除字母数字和重音以外的所有特殊字符 我试过这样的方法: text = 'abcdeáéí.@# ' re.sub(r'[^a-zA-Z0-9áéíóúÁÉÍÓÚâêîôÂÊÎÔãõÃÕçÇ: ]', ' ', text) 但我没有成功。以下表达式仅允许字母数字,但不允许重音符号: tmp = re.sub(r'[^a-zA-Z0-9: ]', '', x) 有人能帮我吗?把你的文本变成一个unicode字符串text=u'abcdeá#í。并确保你的模式也能接受unicode字符re

如何删除除字母数字和重音以外的所有特殊字符

我试过这样的方法:

text = 'abcdeáéí.@# '
re.sub(r'[^a-zA-Z0-9áéíóúÁÉÍÓÚâêîôÂÊÎÔãõÃÕçÇ: ]', ' ', text)
但我没有成功。以下表达式仅允许字母数字,但不允许重音符号:

tmp = re.sub(r'[^a-zA-Z0-9: ]', '', x)

有人能帮我吗?

把你的文本变成一个unicode字符串
text=u'abcdeá#í。
并确保你的模式也能接受unicode字符
re.sub(u'[^a-zA-Z0-9áííÉÉÉÚîØõ195;Õç:','',text)

通过这种组合,我得到了
u'abcde\xe1\xe9\xed'
(其中
\xe1
等是
文本中重音字符的转义码


如果您不转义任何字符,则不需要在模式前面使用
r
。这样您就可以编写类似
r'\d\w'
的内容,而不是
'\\d\\w'

这是什么版本的Python?据我所知,Python 2的正则表达式引擎不支持unicode.Python 2.7.3(默认,2012年7月16日,13:05:29)[GCC 4.5.3]关于Linux2hat的奇数。我做了完全相同的更改,最后的字符串没有重音字符:
print re.sub(u'[^a-zA-Z0-9áaíúúúúúúú218;úúúúúúúúúúúúúúúúúúúúúúúúúúúúúúú
。我从你的代码片段中得到了
abcde
。但是
>打印re.sub(u'[^a-zA-Z0-9áúúúúúúú憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽憽