python中使用正则表达式的连接符示例代码

yipeiwu_com5年前Python基础

前言

我们在前面的例子里,我们学习使用集合里字符或非集合里的字符,这时都是要把每个字符写出来的,但是有时需要把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'

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

相关文章

在python中bool函数的取值方法

bool是Boolean的缩写,只有真(True)和假(False)两种取值 bool函数只有一个参数,并根据这个参数的值返回真或者假。 1.当对数字使用bool函数时,0返回假(Fal...

python采用django框架实现支付宝即时到帐接口

因工作需要研究了支付宝即时到帐接口,并成功应用到网站上,把过程拿出来分享。 即时到帐只是支付宝众多商家服务中的一个,表示客户付款,客户用支付宝付款,支付宝收到款项后,马上通知你,并且此笔...

django使用图片延时加载引起后台404错误

django使用图片延时加载引起后台404错误

环境 django 1.10.6 缘起 今天接到一个任务——解决终端满屏日志中的无用错误。 django 会尽可能给你准确报出错误位置,但是一些复杂,深层次的错误它自带的错误日志有些不足...

Python基于pygame实现的弹力球效果(附源码)

Python基于pygame实现的弹力球效果(附源码)

本文实例讲述了Python基于pygame实现的弹力球效果。分享给大家供大家参考,具体如下: 运行效果: 代码部分如下: #A bouncing ball import sys,...

python修改操作系统时间的方法

本文实例讲述了python修改操作系统时间的方法。分享给大家供大家参考。具体实现方法如下: #-*- coding:utf-8 -*- import socket import st...