本文摘自php中文网,作者黄舟,侵删。
在正则表达式中,匹配数字或者英文字母的书写非常不方便。因此,正则表达式引入了连接符“-”来定义字符的范围,下面这篇文章主要给大家介绍了关于python中如何使用正则表达式的连接符的相关资料,需要的朋友可以参考下。前言
我们在前面的例子里,我们学习使用集合里字符或非集合里的字符,这时都是要把每个字符写出来的,但是有时需要把26个小写字母都放到集合里,那么按集合的方法,得输入26次,一个一个键入去,这样比较花时间,也容易出错,那么有没有更好的方法呢?这个是有的,就是使用正则表达式的连接符的功能:-,比如表示26个小写字符,就使用[a-z]就可以了。
本文详细的给大家介绍了关于python使用正则表达式的连接符的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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' )],
)
|
结果输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | '[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'
|
总结
以上就是python使用正则表达式连接符的示例代码的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python代码程序内存回收机制的实现方法
Python线程下thread对象的用法介绍(附实例)
初学Python看什么书?
Python semaphore(信号量)是什么?(实例详解)
Python中的函数介绍
Python生成随机数的方法_Python
在sublime中如何搭建Python开发环境讲解
Python学哪个web框架好?2019 Python web框架排行
Python如何实现简单的用户交互程序(示例)
Python numpy函数中linspace实现创建等差数列的实例分享
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python使用正则表达式连接符的示例代码