python正则过滤字母、中文、数字及特殊字符方法详解
作者:jlulxg 时间:2022-02-20 10:14:47
去除数字,特殊字符,只保留汉字
import re
s = '1123*#$ 中abc国'
str = re.sub('[a-zA-Z0-9'!"#$%&\'()*+,-./:;<=>?@,。?★、…【】《》?“”‘'![\\]^_`{|}~\s]+', "", s)
# 去除不可见字符
str = re.sub('[\001\002\003\004\005\006\007\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a]+', '', x)
print(str)
# 结果为:中国
去除特殊字符,只保留汉子,字母、数字
import re
string = "123我123456abcdefgABCVDFF?/ ,。,.:;:''';'''[]{}()()《》"
print(string)
123我123456abcdefgABCVDFF?/ ,。,.:;:''';'''[]{}()()《》
sub_str = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","",string)
print(sub_str)
123我123456abcdefgABCVDFF
正则表达式说明
函数 | 说明 |
sub(pattern,repl,string) | 把字符串中的所有匹配表达式pattern中的地方替换成repl |
[^**] | 表示不匹配此字符集中的任何一个字符 |
\u4e00-\u9fa5 | 汉字的unicode范围 |
\u0030-\u0039 | 数字的unicode范围 |
\u0041-\u005a | 大写字母unicode范围 |
\u0061-\u007a | 小写字母unicode范围 |
\uAC00-\uD7AF | 韩文的unicode范围 |
\u3040-\u31FF | 日文的unicode范围 |
来源:https://blog.csdn.net/jlulxg/article/details/84650683
标签:python,正则,中文
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
YUI Grids CSS 解读
2008-05-28 12:49:00
django+echart绘制曲线图的方法示例
2023-04-22 20:56:52
![](https://img.aspxhome.com/file/2023/3/96923_0s.png)
Python中url标签使用知识点总结
2022-05-12 11:13:09
![](https://img.aspxhome.com/file/2023/5/79465_0s.png)
python得到一个excel的全部sheet标签值方法
2022-11-22 15:27:51
python Gunicorn服务器使用方法详解
2021-09-06 00:35:08
![](https://img.aspxhome.com/file/2023/4/81784_0s.jpg)
设计师的幸福
2009-05-21 11:59:00
QML使用Python的函数过程解析
2021-12-10 19:20:31
教你用pyecharts绘制各种图表案例(效果+代码)
2021-07-30 21:56:54
![](https://img.aspxhome.com/file/2023/8/94168_0s.png)
如何用ASP输出HTML文件?
2010-06-11 20:01:00
基于Python的接口自动化读写excel文件的方法
2023-09-04 00:25:10
![](https://img.aspxhome.com/file/2023/0/93970_0s.png)
Python中Matplotlib的点、线形状、颜色以及绘制散点图
2023-06-11 00:32:26
![](https://img.aspxhome.com/file/2023/5/78685_0s.jpg)
Django前后端分离csrf token获取方式
2021-03-15 04:23:43
![](https://img.aspxhome.com/file/2023/3/99143_0s.jpg)
TensorFlow加载模型时出错的解决方式
2023-12-23 05:49:04
使用OpenCV实现人脸图像卡通化的示例代码
2023-01-03 13:38:49
![](https://img.aspxhome.com/file/2023/7/82527_0s.jpg)
jQuery性能优化指南[译]
2009-05-12 11:54:00
教你如何将 Sublime 3 打造成 Python/Django IDE开发利器
2022-10-10 11:37:29
![](https://img.aspxhome.com/file/2023/7/63887_0s.png)
asp MYSQL出现问号乱码的解决方法
2011-04-15 11:13:00
Python实现对百度云的文件上传(实例讲解)
2022-04-16 03:15:46
![](https://img.aspxhome.com/file/2023/1/107681_0s.jpg)
Python进行数据提取的方法总结
2022-06-23 18:02:45
![](https://img.aspxhome.com/file/2023/3/111963_0s.jpg)
一个jquery日期选取插件源码
2009-12-23 19:15:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/2009126213743-92s.png)