python第一个正则表达式

https://www.imooc.com/learn/550

r'imooc'  Pattern Match result

In [2]: import re

In [3]: pa = re.compile(r'imooc')
#re的compile方法生成pattern对象 In [4]: type(pa)
Out[4]: _sre.SRE_Pattern In [5]: pa.
pa.findall pa.fullmatch pa.match pa.search pa.subn
pa.finditer pa.groupindex pa.pattern pa.split
pa.flags pa.groups pa.scanner pa.sub In [8]: str = 'imooc python' In [9]: pa.match(str)
#pattern对象的match方法对字符串进行匹配
Out[9]: <_sre.SRE_Match object; span=(0, 5), match='imooc'>
In [10]: ma = pa.match(str) In [11]: ma.
ma.end ma.group ma.lastgroup ma.re ma.start ma.endpos ma.groupdict
ma.lastindex ma.regs ma.string ma.expand ma.groups ma.pos ma.span In [11]: ma.group()
Out[11]: 'imooc'
#返回匹配到的字符串 In [15]: ma.span()
Out[15]: (0, 5)
#返回匹配到的字符串下标索引
In [1]: import re

In [2]: pa = re.compile(r'imooc',re.I)
#忽略字母大小写 In [3]: pa
Out[3]: re.compile(r'imooc', re.IGNORECASE|re.UNICODE) In [4]: ma = pa.match('imooc python') In [5]: ma.group()
Out[5]: 'imooc' In [6]: ma = pa.match('Imooc python') In [7]: ma.group()
Out[7]: 'Imooc' In [8]: ma = pa.match('ImoOc python') In [9]: ma.group()
Out[9]: 'ImoOc' In [10]: ma.groups()
Out[10]: () In [11]: pa = re.compile(r'(imooc)',re.I) In [12]: ma = pa.match('imooc') In [13]: ma.group()
Out[13]: 'imooc' In [14]: ma.groups()
Out[14]: ('imooc',)
#以分组的形式返回 In [15]: ma = re.match(r'imooc','imooc python')
#compile和match可进行合并 In [16]: ma.group()
Out[16]: 'imooc'

python正则表达式之re模块使用的更多相关文章

  1. python正则表达式之re模块方法介绍

    python正则表达式之re模块其他方法 1:search(pattern,string,flags=0) 在一个字符串中查找匹配 2:findall(pattern,string,flags=0) ...

  2. Python正则表达式与re模块介绍

    Python中通过re模块实现了正则表达式的功能.re模块提供了一些根据正则表达式进行查找.替换.分隔字符串的函数.本文主要介绍正则表达式先关内容以及re模块中常用的函数和函数常用场景. 正则表达式基 ...

  3. python 正则表达式 (重点) re模块

    京东的注册页面,打开页面我们就看到这些要求输入个人信息的提示.假如我们随意的在手机号码这一栏输入一个11111111111,它会提示我们格式有误.这个功能是怎么实现的呢?假如现在你用python写一段 ...

  4. Python正则表达式与hashlib模块

    菜鸟学python第十六天 1.re模块(正则表达式) 什么是正则表达式 正则表达式是一个由特殊字符组成的序列,他能帮助对字符串的某种对应模式进行查找. 在python中,re 模块使其拥有全部的正则 ...

  5. python正则表达式与re模块-02

    正则表达式 正则表达式与python的关系 # 正则表达式不是Python独有的,它是一门独立的技术,所有的编程语言都可以使用正则 # 但要在python中使用正则表达式,就必须依赖于python内置 ...

  6. python 正则表达式re使用模块(match()、search()和compile())

    摘录 python核心编程 python的re模块允许多线程共享一个已编译的正则表达式对象,也支持命名子组.下表是常见的正则表达式属性: 函数/方法 描述 仅仅是re模块函数 compile(patt ...

  7. Python正则表达式与re模块

    在线正则表达式测试 http://tool.oschina.net/regex/ 常见匹配模式 模式 描述 \w 匹配字母数字及下划线 \W 匹配非字母数字下划线 \s 匹配任意空白字符,等价于 [\ ...

  8. python 正则表达式与re模块

    一.正则表达式 用途 用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. #### 简单地说 就是用于字符串匹配的 字符组 在 ...

  9. Python 正则表达式、re模块

    一.正则表达式 对字符串的操作的需求几乎无处不在,比如网站注册时输入的手机号.邮箱判断是否合法.虽然可以使用python中的字符串内置函数,但是操作起来非常麻烦,代码冗余不利于重复使用. 正则表达式是 ...

  10. [ python ] 正则表达式及re模块

    正则表达式 正则表达式描述: 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个‘规则字符串’,这个‘规则字符串’用来    表达对字符串的一种过滤 ...

随机推荐

  1. jsonConfig用法

    1.先编写jsonConfig的初始化代码 private JsonConfig jsonConfig; public action构造方法() { jsonConfig = new JsonConf ...

  2. Reveal详细安装教程

    Reveal的详细安装使用 标签: Reveal 工具 调试 iOS 一.终端的操作 首先最重要的一点,要先把Reveal软件放到Application中,否则路径是错的,后面的设置也就没有作用了 打 ...

  3. [LC] 149. Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  4. 蓝桥杯-PREV31-小朋友排队

    解法: 这题有点像冒泡排序,但是做这题并不需要冒泡排序. 假设第i个小朋友比第j个小朋友高,而且i < j 为了把队伍排成从小到大,第i个小朋友一定要去第j个小朋友的右边.又因为只能交换位置相邻 ...

  5. Nginx笔记总结二十一:隐藏或者混淆nginx返回的Server信息

    [root@localhost nginx-]# vi src/http/ngx_http_header_filter_module.c 修改:49-50行 static char ngx_http_ ...

  6. considerate|considerable|content|Contact|Consult|deceived|

    ADJ-GRADED 替人着想的;体贴的Someone who is considerate pays attention to the needs, wishes, or feelings of o ...

  7. CF580D_Kefa and Dishes

    D. Kefa and Dishes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Acwing 843. n-皇后问题

    n-皇后问题是指将 n 个皇后放在 n∗n 的国际象棋棋盘上,使得皇后不能相互攻击到,即任意两个皇后都不能处于同一行.同一列或同一斜线上. 现在给定整数n,请你输出所有的满足条件的棋子摆法. 输入格式 ...

  9. Numpy入门(三):Numpy概率模块和线性代数模块

    Numpy中经常使用到的两个模块是概率模块和线性代数模块,random 和 linalg 两个模块. 概率模块 产生二项分布的随机数:np.random.binomial(n,p,size=-),其中 ...

  10. mysqldump免密备份方法

    注意:1.暂时只试验了root用户     2.暂时只试验了5.6和5.7两个版本 1.我用的root用户,先进入家目录 cd ~ 2.vim .my.cnf #在家目录添加该文件 [mysqldum ...