Python在字符处理方面拥有很强大的优势,其中正则表达式是其中的精髓。可以利用正则表达式对字符串进行特定规则的查找,分割。本文对python在正则表达式方面的应用做了一个基本的总结。

python的re模块就是专门用于正则的模块,该模块主要有两部分组成。第一部分是正则匹配的规则,第二部分是re的相关函数。在引用这个模块之前,需要先导入该模块。

  1. 正则匹配特殊字符

匹配规则需要知道一些常用匹配字符的定义,常见的匹配字符归纳如下:

字符  功能 
. 匹配除换行符'/n'外的所有字符
* 匹配前面规则0次或多次
+ 匹配前面规则1次多多次
| 或规则,表示满足其一就匹配,例如:dog|cat表示匹配'dog'和'cat'
? 匹配前面规则0次或1次
^ 匹配字符串的开头
$ 匹配字符串的结尾
[] 字符集合,例如[abc],表示'a','b','c'都可以匹配
() 组,通常把相关的匹配规则写在组里
/d 匹配一个数字,/D表示取反,匹配一个非数字
/w 匹配所有英文字母和数字,/W是/w的取反
/s 匹配空格符,制表符,回车符等表示空格意义的字符
(?:) 重复字符匹配,例如(?:ab)表示匹配字符中重复的ab

2. re常用函数

  • findall(pattern,string,flags=0)

在string中查找pattern,返回结果是一个列表,存放符合规则的字符串,若没有找到找到符合规则的字符串,返回一个空列表。

  • compile(pattern, flags=0)

将正则规则编译成一个pattern对象,以供后续使用。返回一个pattern对象。

  • match(pattern,string,flags=0)

用pattern去匹配string的开头部分,若成功,返回一个match object, 不成功则返回一个None.

  • search(pattern,string, flags=0)

用pattern去匹配string,若成功,返回一个match object, 不成功则返回一个None

  • split(pattern, string, flags=0)

用pattern规则在string中查找匹配的字符串,并用该字符作为分界,分割string,f返回一个列表。

3. 代码

用下面的例子简要介绍正则的相关应用并对代码做了一些注解。

import re #import re module

import string

txt = "Jeffrey is an engineer and work for NSN, he is cool, clever, and very smart....Cool! This is so wonderful word!"

txtlist = txt.split() # string.split module: split the txt using space as default, you can also use '\n',''\r', '\t', or even letters.

print txt

print txtlist

regexfortxt = re.compile('cool') #create a regex object for many operations

print regexfortxt.findall(txt) #find the regex in txt and return a list of non-overlapping matches in the string

print regexfortxt.search(txt) # scan through string looking for a match to the pattern, returning a match object, or None of no match was found

if regexfortxt.search(txt): # using the result of re.search to decide which step should be proceed

print "there is a regexfortxt in txt!"

else:

print "there is no a regexefortxt in txt!"

print regexfortxt.match(txt) # you can check that re.match only check the start of string and will return None when cannot find regex at the start of txt

regexsplit = regexfortxt.split(txt) #re.split module: split the string by the occurrences of pattern, return a list

print regexsplit

print len(regexsplit)

print '\n'

regexforlist = re.compile('(^.*er.*$|^.*re.*$)') # pattern a string which contain 'er' or 're' in it.

i = 0

while(i<len(txtlist)):

if regexforlist.search(txtlist[i]): # re.search is to pattern the list item with regex

print txtlist[i]

i = i + 1

continue

else:

i = i + 1

4. 输出

5. 注意

  • re.search 和re.match的区别: 正如前面r常用模块所说,search函数是用规则去匹配整个string, 而match只匹配string的开头部分。

  • 正则匹配只能匹配字符串类型的数据,也就是type()为'str'的数据,如果是需要去匹配一个列表,也就是type()为'list'的数据,需要分别匹配list里面的每个item, 因此要做一个循环语句来实现。

转载于:https://blog.51cto.com/7731023/1269461

Python 正则模块的应用相关推荐

  1. python正则模块re

    python正则模块re 一.re模块内置的函数方法 re.compile(pattern, flags=0) https://cdn.analyticsvidhya.com/wp-content/u ...

  2. python正则模块,match()函数

    # 在Python中需要通过正则表达式对字符串进⾏匹配的时候,可以使⽤⼀个python自带的模块,名字为re. # # 正则表达式的大致匹配过程是: # 1.依次拿出表达式和文本中的字符比较, # 2 ...

  3. python中search用法_Python3中正则模块re.compile、re.match及re.search函数用法详解

    本文实例讲述了Python3中正则模块re.compile.re.match及re.search函数用法.分享给大家供大家参考,具体如下: re模块 re.compile.re.match. re.s ...

  4. python re正则_正则表达式+Python re模块详解

    正则表达式(Regluar Expressions)又称规则表达式,在代码中常简写为REs,regexes或regexp(regex patterns).它本质上是一个小巧的.高度专用的编程语言. 通 ...

  5. Python正则re模块详解

    Cooking Regex微信公众号 正则表达式学习 目录 re.match re.fullmatch re.search re.sub re.subn re.split re.findall re. ...

  6. python re正则模块详解

    一. 正则表达式 正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串.将匹配的子串替换或者从某个串中取出符合某个条件的子 ...

  7. python中import re_Python3中正则模块re.compile、re.match及re.search函数用法详解

    本文实例讲述了Python3中正则模块re.compile.re.match及re.search函数用法.分享给大家供大家参考,具体如下: re模块 re.compile.re.match. re.s ...

  8. Python Re 模块超全解读!详细

    内行必看!Python Re 模块超全解读! 2019.08.08 18:59:45字数 953阅读 121 re模块下的函数 compile(pattern):创建模式对象 > import ...

  9. Day05 - Python 常用模块

    1. 模块简介 模块就是一个保存了 Python 代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码. 模块也是 Python 对象,具有随机的名字属性用来绑定或引用. 下例是个简单的模 ...

最新文章

  1. SystemCenter2012SP1实践(27)VMM和HyperV的PowerShell
  2. 自定义Xcode 文件头部的注释
  3. dicom文件_DICOM数据转成NIfTI数据
  4. 直接通过ADO操作Access数据库(修改版)
  5. 大数据_MapperReduce_Hbase的优化_高可用 预分区_防止数据倾斜_JAVAAPI创建预分区---Hbase工作笔记0026
  6. mysql 1.6安装_centos 6.6编译安装nginx1.6.2+mysql5.6.21+php5.6.3.docx
  7. IIS7配置Gzip压缩
  8. Redis命令拾遗四——集合类型(命令补充)
  9. Zemax—波长1550nm不在所选玻璃色散公式的有效范围内
  10. 仿迅雷播放器教程 -- 提取exe资源(12)
  11. Java项目:房产中介管理系统(java+SSM+HTML+bootstrap+layui+Mysql)
  12. 性能、应用、安装,选择LoRaWAN温湿度传感器的关键
  13. linux uefi iso,支持UEFI启动的GRUB2 ISO光盘镜像的制作
  14. 什么是云桌面?云桌面的三大基本架构组成部分
  15. 如何在一张ppt中插入多张图片并能依次播放
  16. 摄影测量学空间后方交会
  17. Google地图之野望:你所不知道的背后故事
  18. ts服务器cal文件激活,付费网课ts文件如何解密?m3u8无IV信息
  19. python:unsupported format character
  20. python htmlparser怎么用,在Python中高效地使用HTMLParser

热门文章

  1. Unity 之 Shader 面的剔除 Cull
  2. 利用linux curl爬取网站数据
  3. WCF与AJAX编程开发实践(1):AJAX基础概念和纯AJAX示例
  4. WebSocket能干啥
  5. Spring Boot+Maven将配置文件打包到Jar包外方便运维修改配置
  6. 从源码分析RocketMQ系列-消息拉取PullMessageProcessor详解
  7. SpringBoot配置@PropertySource、@ImportResource、@Bean注解
  8. java返回泛型_Java泛型从泛型方法返回持有者对象
  9. matplotlib中文乱码问题_MacOS解决Matplotlib的中文乱码问题
  10. Spring 事务初始化源码分析