我需要从字符串中删除所有特殊字符,标点符号和空格,以便我只有字母和数字。
当前回答
下面是一个正则表达式,用于匹配不是字母或数字的字符串:
[^A-Za-z0-9]+
下面是执行正则表达式替换的Python命令:
re.sub('[^A-Za-z0-9]+', '', mystring)
其他回答
import re
my_string = """Strings are amongst the most popular data types in Python. We can create the strings by enclosing characters in quotes. Python treats single quotes the
和双引号一样。”""
# if we need to count the word python that ends with or without ',' or '.' at end
count = 0
for i in text:
if i.endswith("."):
text[count] = re.sub("^([a-z]+)(.)?$", r"\1", i)
count += 1
print("The count of Python : ", text.count("python"))
import re
abc = "askhnl#$%askdjalsdk"
ddd = abc.replace("#$%","")
print (ddd)
你会看到你的结果是
'Askhnlaskdjalsdk
#!/usr/bin/python
import re
strs = "how much for the maple syrup? $20.99? That's ricidulous!!!"
print strs
nstr = re.sub(r'[?|$|.|!]',r'',strs)
print nstr
nestr = re.sub(r'[^a-zA-Z0-9 ]',r'',nstr)
print nestr
你可以添加更多的特殊字符,这将被“意味着什么,即他们将被删除”所取代。
下面是一个正则表达式,用于匹配不是字母或数字的字符串:
[^A-Za-z0-9]+
下面是执行正则表达式替换的Python命令:
re.sub('[^A-Za-z0-9]+', '', mystring)
使用翻译:
import string
def clean(instr):
return instr.translate(None, string.punctuation + ' ')
警告:仅适用于ascii字符串。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- Printf与std::字符串?
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if