python 用所有标点符号分隔句子的示例
作者:HappyRocking 时间:2022-09-18 01:41:57
问题
给出一段话,由短句组成,短句之间可能被任意标点符号隔开。想要提取所有的短句。
解决
使用 re.split 函数,用正则式匹配的方法,一次性分隔所有短句。
import re
pattern = r',|\.|/|;|\'|`|\[|\]|<|>|\?|:|"|\{|\}|\~|!|@|#|\$|%|\^|&|\(|\)|-|=|\_|\+|,|。|、|;|‘|'|【|】|·|!| |…|(|)'
test_text = 'b,b.b/b;b\'b`b[b]b<b>b?b:b"b{b}b~b!b@b#b$b%b^b&b(b)b-b=b_b+b,b。b、b;b‘b'b【b】b·b!b b…b(b)b'
result_list = re.split(pattern, test_text)
print(result_list)
输出为
['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']
可见所有的 b 都被提取了出来。
来源:https://blog.csdn.net/HappyRocking/article/details/80900890
标签:python,标点符号,分隔,句子
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中使用asyncio实现异步IO实例分析
2021-02-06 10:02:50
Python的matplotlib绘图如何修改背景颜色的实现
2023-10-05 08:12:43
![](https://img.aspxhome.com/file/2023/0/62900_0s.png)
python实现对服务器脚本敏感信息的加密解密功能
2022-11-07 08:35:11
深入解析Go语言编程中的递归使用
2023-10-09 09:24:36
Python编程之基于概率论的分类方法:朴素贝叶斯
2023-10-08 10:15:52
![](https://img.aspxhome.com/file/2023/2/63262_0s.png)
简单理解PHP的面向对象编程方式
2023-06-13 15:38:08
php中Array2xml类实现数组转化成XML实例
2023-07-14 21:48:13
Python API 操作Hadoop hdfs详解
2023-02-24 02:23:26
简单的Python解密rsa案例
2022-05-28 07:30:18
![](https://img.aspxhome.com/file/2023/9/65109_0s.png)
SQL语句中的一些特殊参数如何用变量来代替
2008-03-14 07:44:00
详解python UDP 编程
2023-06-11 22:31:44
![](https://img.aspxhome.com/file/2023/5/59825_0s.png)
正则表达式语法速查
2007-11-11 10:43:00
在JScript中使用RecordSet对象的GetRows方法
2008-01-16 13:12:00
python中的class_static的@classmethod的巧妙用法
2022-07-18 15:54:43
ASP XML操作类代码
2011-03-08 10:47:00
Python遍历文件夹和读写文件的实现代码
2023-07-28 12:13:10
![](https://img.aspxhome.com/file/2023/4/60514_0s.jpg)
详解Python3中的迭代器和生成器及其区别
2022-11-01 00:37:48
![](https://img.aspxhome.com/file/2023/6/72206_0s.png)
[整理版]防止Access数据库被下载的9种方法
2007-08-10 09:31:00
FCKeditor编辑器实战技巧
2007-10-08 21:13:00
![](https://img.aspxhome.com/file/UploadPic/200710/8/2007108211717673s.gif)
SQL语句之WHERE子句的使用方法
2007-08-11 12:25:00