python中使用正则表达式的连接符示例代码
作者:caimouse 时间:2021-07-08 09:34:43
前言
我们在前面的例子里,我们学习使用集合里字符或非集合里的字符,这时都是要把每个字符写出来的,但是有时需要把26个小写字母都放到集合里,那么按集合的方法,得输入26次,一个一个键入去,这样比较花时间,也容易出错,那么有没有更好的方法呢?这个是有的,就是使用正则表达式的连接符的功能:-,比如表示26个小写字符,就使用[a-z]就可以了。
本文详细的给大家介绍了关于python使用正则表达式的连接符的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
例子如下:
#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
from re_test_patterns import test_patterns
test_patterns(
'This is some text -- with punctuation.',
[('[a-z]+', 'sequences of lowercase letters'),
('[A-Z]+', 'sequences of uppercase letters'),
('[a-zA-Z]+', 'sequences of letters of either case'),
('[A-Z][a-z]+', 'one uppercase followed by lowercase')],
)
结果输出如下:
'[a-z]+' (sequences of lowercase letters)
'This is some text -- with punctuation.'
.'his'
.....'is'
........'some'
.............'text'
.....................'with'
..........................'punctuation'
'[A-Z]+' (sequences of uppercase letters)
'This is some text -- with punctuation.'
'T'
'[a-zA-Z]+' (sequences of letters of either case)
'This is some text -- with punctuation.'
'This'
.....'is'
........'some'
.............'text'
.....................'with'
..........................'punctuation'
'[A-Z][a-z]+' (one uppercase followed by lowercase)
'This is some text -- with punctuation.'
'This'
来源:http://blog.csdn.net/caimouse/article/details/78174162
标签:python,正则表达式,连接符
0
投稿
猜你喜欢
一篇文章弄懂Python关键字、标识符和变量
2022-11-01 17:44:42
Python 运行 shell 获取输出结果的实例
2023-08-02 16:51:18
Python实现计算文件夹下.h和.cpp文件的总行数
2022-09-20 00:54:51
恢复master..xp_logattach(log explorer)
2010-07-01 19:19:00
Python datetime时间格式化去掉前导0
2022-03-15 11:43:15
python自动生成model文件过程详解
2023-09-30 02:54:05
如何基于Python实现自动扫雷
2023-12-13 15:51:08
【推荐】汉字转为拼音的asp函数
2007-08-17 11:11:00
深入理解Django的自定义过滤器
2021-01-25 04:01:54
Python函数式编程指南(三):迭代器详解
2023-06-03 06:11:52
Python实现人脸识别
2022-10-01 06:06:24
浅析阿里巴巴前端招聘考题
2008-01-19 09:52:00
20个Javascript手风琴折叠菜单
2009-10-12 12:09:00
在python中将list分段并保存为array类型的方法
2023-11-15 10:18:00
python内存管理机制原理详解
2021-05-24 16:19:48
Python爬虫网页元素定位术
2023-07-25 16:28:00
利用Python程序读取Excel创建折线图
2021-05-29 00:36:33
numpy 声明空数组详解
2022-09-20 15:29:44
10个杀手级应用的Python自动化脚本
2023-10-06 14:01:53
Python 私有化操作实例分析
2022-11-06 05:44:42