从字符串中删除所有特殊字符、标点符号和空格 [英] Remove all special characters, punctuation and spaces from string

查看:147
本文介绍了从字符串中删除所有特殊字符、标点符号和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从字符串中删除所有特殊字符、标点符号和空格,以便我只有字母和数字.

解决方案

无需正则表达式即可:

<预><代码>>>>string = "特殊的 $#! 字符空格 888323">>>''.join(e for e in string if e.isalnum())'特殊字符空格888323'

您可以使用 str.isalnum:

<块引用>

S.isalnum() ->布尔值如果 S 中的所有字符都是字母数字,则返回 TrueS 中至少有一个字符,否则为 False.

如果您坚持使用正则表达式,其他解决方案也可以.但是请注意,如果可以在不使用正则表达式的情况下完成,那是最好的方法.

I need to remove all special characters, punctuation and spaces from a string so that I only have letters and numbers.

解决方案

This can be done without regex:

>>> string = "Special $#! characters   spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'

You can use str.isalnum:

S.isalnum() -> bool

Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.

If you insist on using regex, other solutions will do fine. However note that if it can be done without using a regular expression, that's the best way to go about it.

这篇关于从字符串中删除所有特殊字符、标点符号和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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