#coding:utf-8
import re #将正则表达式编译为pattern对象
#compile(pattern, flags=0)
#Compile a regular expression pattern, returning a pattern object.
pattern = re.compile(r'sub2020')
#help(re.match) Try to apply the pattern at the start of the string
#match 从 str 的开始(0 位置)查找匹配,如果没有,返回none
#help(re.search) Scan through string looking for a match to the pattern
#search 扫描整个 str
match =re.search(pattern,'http://www.cnblogs.com/sub2020/p/7920845.html') if match:
#使用match获得group信息
print match.group() m=re.search(r'(\w+)(\w+)(\w+)(\d+)(\d+)(\d+)(\d+)','http://www.cnblogs.com/sub2020/p/7920845.html') #string: 匹配时使用的文本。
print 'm.string :',m.string
#re: 匹配时使用的Pattern对象
print 'm.re :',m.re
#pos: 文本中正则表达式开始搜索的索引。值与Pattern.match()
# 和Pattern.seach()方法的同名参数相同。
print 'm.pos :',m.pos
#endpos: 文本中正则表达式结束搜索的索引。值与Pattern.match()
# 和Pattern.seach()方法的同名参数相同。
print 'm.endpos :',m.endpos
#lastindex: 最后一个被捕获的分组在文本中的索引。
# 如果没有被捕获的分组,将为None。
print 'm.lastindex :',m.lastindex
#lastgroup: 最后一个被捕获的分组的别名。
# 如果这个分组没有别名或者没有被捕获的分组,将为None。
print 'm.lastgroup :',m.lastgroup
#.group([group1, …]):获得一个或多个分组截获的字符串;
# 指定多个参数时将以元组形式返回。group1可以使用编号也可以使用别名;
# 编号0代表整个匹配的子串;不填写参数时,返回group(0);
# 没有截获字符串的组返回None;截获了多次的组返回最后一次截获的子串。
print 'm.group() :',m.group()
print 'm.group(1,2) :',m.group(1,2)
print 'm.group(1, 2) :',m.group(1, 2)
#groups([default]):以元组形式返回全部分组截获的字符串。
# 相当于调用group(1,2,…last)。default表示没有截获字符串的组以这个
# 值替代,默认为None。
print 'm.groups() :',m.groups()
print 'm.groups(3) :',m.groups(3)
#groupdict([default]):返回以有别名的组的别名为键、以该组截获的子串
# 为值的字典,没有别名的组不包含在内。default含义同上。
print 'm.groupdict() :',m.groupdict()
#start([group]):返回指定的组截获的子串在string中的起始索引
#(子串第一个字符的索引)。group默认值为0。
print 'm.start(2) :',m.start(2)
print 'm.start(3) :',m.start(3)
#end([group]):返回指定的组截获的子串在string中的结束索引
#(子串最后一个字符的索引+1)。group默认值为0。
print 'm.end(2) :',m.end(2)
print 'm.end(3) :',m.end(3)
#span([group]):返回(start(group), end(group))。
print 'm.span(1) :',m.span(1)
print 'm.span(2) :',m.span(2)
#expand(template):将匹配到的分组代入template中然后返回。
# template中可以使用\id或\g、\g引用分组,但不能使用编号0。
# \id与\g是等价的;但\10将被认为是第10个分组,如果你想表达\1之后是字符’0’,
# 只能使用\g0。
print r"m.expand(r'\g') 1:",m.expand(r'\1')
print r"m.expand(r'\g') 2:",m.expand(r'\2')
print r"m.expand(r'\g') 3:",m.expand(r'\3') print r"m.expand(r'\g \g\g') :",m.expand(r'\2 \2\2')
print r"m.expand(r'\g \g\g') :",m.expand(r'\2 \1\3')

Output:

 sub2020
m.string : http://www.cnblogs.com/sub2020/p/7920845.html
m.re : <_sre.SRE_Pattern object at 0x0000000001D08810>
m.pos : 0
m.endpos : 45
m.lastindex : 7
m.lastgroup : None
m.group() : sub2020
m.group(1,2) : ('s', 'u')
m.group(1, 2) : ('s', 'u')
m.groups() : ('s', 'u', 'b', '', '', '', '')
m.groups(3) : ('s', 'u', 'b', '', '', '', '')
m.groupdict() : {}
m.start(2) : 24
m.start(3) : 25
m.end(2) : 25
m.end(3) : 26
m.span(1) : (23, 24)
m.span(2) : (24, 25)
m.expand(r'\g') 1: s
m.expand(r'\g') 2: u
m.expand(r'\g') 3: b
m.expand(r'\g \g\g') : u uu
m.expand(r'\g \g\g') : u sb ***Repl Closed***

quote:http://cuiqingcai.com/977.html

python 正则表达式 re.search的更多相关文章

  1. Python正则表达式re.search(r'\*{3,8}','*****')和re.search('\*{3,8}','*****')的匹配结果为什么相同?

    老猿做过如下测试: >>> re.search(r'\*{3,100}','*****') <re.Match object; span=(0, 5), match='**** ...

  2. python正则表达式--match search方法

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

  3. python正则表达式(4)--search方法

    1.re.search函数 re.search 扫描整个字符串并返回第一个成功的匹配,如果匹配失败search()就返回None. (1)函数语法: re.search(pattern, string ...

  4. Python正则表达式(总)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6445044.html 本文出自:[Edwin博客园] Python正则表达式(总) search(patte ...

  5. Python正则表达式re模块学习遇到的问题

    Python正则表达式处理的组是什么? Python正则表达式处理中的匹配对象是什么? Python匹配对象的groups.groupdict和group之间的关系 Python正则表达式re.mat ...

  6. python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别

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

  7. Learning Python 008 正则表达式-003 search()方法

    Python 正则表达式 - search()方法 findall()方法在找到第一个匹配之后,还会继续找下去,findall吗,就是找到所有的匹配的意思.如果你只是想找到第一个匹配的信息后,就不在继 ...

  8. Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  9. Python 正则表达式入门(初级篇)

    Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...

随机推荐

  1. Bootstrap前端框架快速入门专题

    1.Bootstrap简介 Bootstrap,出自自 Twitter,是目前最受欢迎的前端框架. Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的前端框架,它简洁灵活,使得 W ...

  2. 个人智能家居系统 - MQTT服务器搭建(centOS7.3)

    个人智能家居系统 - MQTT服务器搭建(centOS7.3) 0x00 参考 在CentOS7 上安装mosquitto1.4.1服务器,实现MQTT信息推送功能并增加websocket功能 mos ...

  3. 面试经典&&竞赛——二叉树

    To record her trees for future generations, she wrote down two strings for each tree: a preorder tra ...

  4. MySQL新增用户及赋予权限

    创建用户 USE mysql; #创建用户需要操作 mysql 表 # 语法格式为 [@'host'] host 为 'localhost' 表示本地登录用户,host 为 IP地址或 IP 地址区间 ...

  5. bzoj4817 & loj2001 [Sdoi2017]树点涂色 LCT + 线段树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4817 https://loj.ac/problem/2001 题解 可以发现这个题就是 bzo ...

  6. Spring Boot日志处理

    2.4 日志处理 2.4.1 记录日志内容 请求url 访问者ip 调用方法classMethod 参数args 返回内容 2.4.2 新建包aspect,新建日志切面处理类 package com. ...

  7. Python中decode与encode的区别

    摘抄: 字符串在Python内部的表示是Unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符解码(decode)成unicode,再从unicode编码 ...

  8. H5 图片上传

    1.h5 图片异步上传 (1) 异步上传input触发onchange事件的时候,就把图片上传至服务器.后台可能会返回图片的链接等信息,前台可以把图片信息展示给用户看. (2) 另一种情况可能需要前台 ...

  9. React Native 之项目的启动

    运行项目有两种方法 1. 到根目录,执行 react-native run-ios 命令 会开启一个本地服务,加载jsbundle文件,然后是去index.js文件 import {AppRegist ...

  10. 8:Spring Boot Shiro记住密码

    1,添加Cookie 2,添加安全管理器中 3,配置记住我, 4 ,在登录页面中加我rememberMe复选框 /** * 1.配置Cookie对象 * 记住我的cookie:rememberMe * ...