Python删除任何不是字母或数字的东西 [英] Python remove anything that is not a letter or number

查看:59
本文介绍了Python删除任何不是字母或数字的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Python 正则表达式时遇到了一些麻烦.

删除字符串中所有非字母或数字字符的好方法是什么?

谢谢!

解决方案

[\w] 匹配(字母数字或下划线).

[\W] 匹配(非(字母数字或下划线)),相当于(非字母数字且非下划线)

您需要 [\W_] 来删除所有非字母数字.

当使用 re.sub() 时,如果你通过使用 [\W_]+ 匹配而不是一次做一个来减少替换的次数(昂贵的)会更有效率.

现在您只需要定义字母数字:

str 对象,只有 ASCII A-Za-z0-9:

 re.sub(r'[\W_]+', '', s)

str 对象,只有语言环境定义的字母数字:

 re.sub(r'[\W_]+', '', s, flags=re.LOCALE)

unicode 对象,所有字母数字:

 re.sub(ur'[\W_]+', u'', s, flags=re.UNICODE)

str 对象的示例:

<预><代码>>>>导入重新,语言环境>>>sall = ''.join(chr(i) for i in xrange(256))>>>伦(萨尔)256>>>re.sub('[\W_]+', '', sall)'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'>>>re.sub('[\W_]+', '', sall, flags=re.LOCALE)'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'>>>locale.setlocale(locale.LC_ALL, '')'English_Australia.1252'>>>re.sub('[\W_]+', '', sall, flags=re.LOCALE)'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\x83\x8a\x8c\x8e\x9a\x9c\x9e\x9f\xaa\xb2\xb3\xb5\xb9\xba\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'# 以上输出包装在第 80 列

Unicode 示例:

<预><代码>>>>re.sub(ur'[\W_]+', u'', u'a_b A_Z \x80\xFF \u0404', flags=re.UNICODE)u'abAZ\xff\u0404'

I'm having a little trouble with Python regular expressions.

What is a good way to remove all characters in a string that are not letters or numbers?

Thanks!

解决方案

[\w] matches (alphanumeric or underscore).

[\W] matches (not (alphanumeric or underscore)), which is equivalent to (not alphanumeric and not underscore)

You need [\W_] to remove ALL non-alphanumerics.

When using re.sub(), it will be much more efficient if you reduce the number of substitutions (expensive) by matching using [\W_]+ instead of doing it one at a time.

Now all you need is to define alphanumerics:

str object, only ASCII A-Za-z0-9:

    re.sub(r'[\W_]+', '', s)

str object, only locale-defined alphanumerics:

    re.sub(r'[\W_]+', '', s, flags=re.LOCALE)

unicode object, all alphanumerics:

    re.sub(ur'[\W_]+', u'', s, flags=re.UNICODE)

Examples for str object:

>>> import re, locale
>>> sall = ''.join(chr(i) for i in xrange(256))
>>> len(sall)
256
>>> re.sub('[\W_]+', '', sall)
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
>>> re.sub('[\W_]+', '', sall, flags=re.LOCALE)
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
>>> locale.setlocale(locale.LC_ALL, '')
'English_Australia.1252'
>>> re.sub('[\W_]+', '', sall, flags=re.LOCALE)
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\x83\x8a\x8c\x8e\
x9a\x9c\x9e\x9f\xaa\xb2\xb3\xb5\xb9\xba\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\
xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde\
xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\
xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
# above output wrapped at column 80

Unicode example:

>>> re.sub(ur'[\W_]+', u'', u'a_b A_Z \x80\xFF \u0404', flags=re.UNICODE)
u'abAZ\xff\u0404'

这篇关于Python删除任何不是字母或数字的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆