一、re的match与search方法

  1.re.match方法  

  re.match 尝试从字符串的起始位置匹配一个模式,匹配成功re.match方法返回一个匹配的对象,如果不是起始位置匹配成功的话,match()就返回none。函数语法:

re.match(pattern, string[, flags])  

  函数参数说明:

  pattern:匹配的正则表达式

  string:要匹配的字符

  flags:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等

  2.match对象

  可以使用group() 、 groups()、groupdict() 匹配对象函数来获取匹配表达式。

  group([group1, …]): 获得一个或多个分组截获的字符串;指定多个参数时将以元组形式返回。group1可以使用编号也可以使用别名;编号0代表整个匹配的子串;不填写参数时,返回group(0);没有截获字符串的组返回None;截获了多次的组返回最后一次截获的子串

  groups([default]): 以元组形式返回全部分组截获的字符串。相当于调用group(1,2,…last)。default表示没有截获字符串的组以这个值替代,默认为None。

  groupdict([default]): 返回以有别名的组的别名为键、以该组截获的子串为值的字典,没有别名的组不包含在内。default含义同上。

  

  3.re.search方法

  re.search 扫描整个字符串并返回第一个成功的匹配。匹配成功re.search方法返回一个匹配的对象,否则返回None。函数语法:

re.search(pattern, string, flags=0)

  参数说明:

  pattern:匹配的正则表达式

  string:要匹配的字符

  flags:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等

  程序例子:

  match与search方法的区别:

import re
reg = re.compile(r'(hello w.*)(hello l.*)')
print (dir(reg))
a = 'hello world hello ling'
result = reg.match(a)
print (result)
print (result.group()) b='aa'+a
print (b)
result2 = reg.match(b)
print (result2) #正则对象的search
print ('###############search########################')
result3 = reg.search(b)
print (result3)
print (result3.group())

结果:

  group() 、 groups()、groupdict()三种获取方式的区别:

import re
prog = re.compile(r'(?P<tagname>abc)(.*)(?P=tagname)')
result = prog.match('abclfjlad234sjldabc') print(dir(result)) print ('##########groups()##############')
print (result.groups()) print ('##########group()##############')
print (result.group(2))
print (result.group(1))
print (result.group('tagname')) print ('##########groupdict()##############')
print (result.groupdict())

结果:

二、re的split、findall、finditer方法 

  split(string[, maxsplit]):按照能够匹配的子串将string分割后返回列表。maxsplit用于指定最大分割次数,不指定将全部分割。

  findall(string[, pos[, endpos]]) :搜索string,以列表形式返回全部能匹配的子串.

  finditer(string[, pos[, endpos]]):搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器。

  程序例子如下:

import re

p1 = re.compile(r'\d+')
a_str = 'one1two2three3four4' #正则对象的split方法,使用正则匹配进行分割字符串
#以列表的形式返回
print(p1.split(a_str)) #正则对象的findall方法,来查找符合对象的字符串
#最后是以列表的形式返回
print (p1.findall(a_str)) for i in p1.finditer(a_str):
print (i.group())

结果:

Python的re模块的常用方法的更多相关文章

  1. python中os模块的常用方法

    1.os模块:os模块在python中包含普遍的操作系统功能,下面列出了一些在os模块中比较有用的部分. os.sep可以取代操作系统特定的路径分隔符.windows下为 “\\” os.name字符 ...

  2. python的xlwt模块的常用方法

    工作中需要导出数据为excel格式,使用了xlwt模块,在此记录一下用到的内容. 1. 创建一个表,设置一个sheet import xlwt workbook = xlwt.Workbook(enc ...

  3. python中calendar模块的常用方法

    >>> import calendar >>> calendar.isleap(2000) #判断是否是闰年 True >>> calendar. ...

  4. 【308】Python os.path 模块常用方法

    参考:Python os.path 模块 参考:python3中,os.path模块下常用的用法总结 01   abspath 返回一个目录的绝对路径. 02   basename 返回一个目录的基名 ...

  5. 【Python之路】第六篇--Python基础之模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  6. python函数,模块及eclipse使用

    一.eclipse的使用 1.作用 (1)最好用的IDE (2)可调式debug (3)查看可执行过程 (4)可查看源代码 2.安装eclipse及配置 目录安装Pythonpython for ec ...

  7. Python中collections模块

    目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...

  8. 周末班:Python基础之模块

    什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...

  9. python的datetime模块处理时间

    python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...

随机推荐

  1. (第三场) A PACM Team 【dp,五维背包】

    链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  2. 【PHP后台】接入支付宝

     我使用PHP主要是为客户端做后台使用,并不会做前端网页.   这两天因为公司项目需要,必须接入支付功能,而支付宝当然首当其冲,考虑迭代版本的需要,首先接入支付宝功能,其他的支付功能以后迭代版本的时候 ...

  3. 应用性能指数(APDEX)是如何计算出来的?

    应用性能指数(APDEX)是如何计算出来的?在应用性能管理领域聚合指标是一种常见手段,主要是用来把成百上千的指标通过某种计算方法聚合成一个或几个指标,用来反映应用的整体健康状态.在这些聚合指标中,比较 ...

  4. css的基础用法(下)

    定位: <html> <head> <meta charset="utf-8" /> <title>定位</title> ...

  5. java 后台返回文件流到浏览器

    package com.springbootblog.controller; import io.swagger.annotations.ApiImplicitParam;import io.swag ...

  6. HTML页面常用的编辑框

    public class FormInputUtil { /** * 获取表单中的InputText * * @param name * @param rs * @return */ public s ...

  7. CALayer简介(转)

    一.简单介绍  在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮,一个文本标签,一个文本输入框,一个图标等等,这些都是UIView.  其实UIView之所以能显示在屏幕上,完全 ...

  8. Vue组件:组件的动态添加与删除

    一.实现效果 二.实现代码 HelloWorld.vue <template> <div class="hello"> <child-page v-f ...

  9. 对象API

    遍历对象里的每个元素 var obj ={ a:32, b:12, c :342 } for (const key of obj){ if(obj.hasOwnProperty(key)){ cons ...

  10. ABAP术语-Call Transaction

    Call Transaction 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/15/1039270.html A data transfe ...