phython正则表达式 Python Re模块

反斜杠问题
与大多数编程语言相同,正则表达式里使用”\”作为转义字符,这就可能造成反斜杠困扰。假如你需要匹配文本中的字符”\”,
Python里的原生字符串很好地解决了这个问题,这个例子中的正则表达式可以使用r”\\”表示。同样,匹配一个数字的”\\d”可以写成r”\d”。有了原生字符串,妈妈也不用担心是不是漏写了反斜杠,写出来的表达式也更直观勒。
获得这个匹配模式:需要利用re.compile方法就可以
__author__ = 'CQC'
# -*- coding: utf- -*- #导入re模块
import re # 将正则表达式编译成Pattern对象,注意hello前面的r的意思是“原生字符串”
pattern = re.compile(r'hello') # 使用re.match匹配文本,获得匹配结果,无法匹配时将返回None
result1 = re.match(pattern,'hello')
result2 = re.match(pattern,'helloo CQC!')
result3 = re.match(pattern,'helo CQC!')
result4 = re.match(pattern,'hello CQC!') #如果1匹配成功
if result1:
# 使用Match获得分组信息
print result1.group()//默认为组0
else:
print '1匹配失败!' #如果2匹配成功
if result2:
# 使用Match获得分组信息
print result2.group()
else:
print '2匹配失败!' #如果3匹配成功
if result3:
# 使用Match获得分组信息
print result3.group()
else:
print '3匹配失败!' #如果4匹配成功
if result4:
# 使用Match获得分组信息
print result4.group()
else:
print '4匹配失败!'
属性:
1.string: 匹配时使用的文本 是原来的文本~
2.re: 匹配时使用的Pattern对象。
4.endpos: 文本中正则表达式结束搜索的索引。值与Pattern.match()和Pattern.seach()方法的同名参数相同。
5.lastindex: 最后一个被捕获的分组在文本中的索引。如果没有被捕获的分组,将为None。
# 匹配如下内容:单词+空格+单词+任意字符
match()函数只检测re是不是在string的开始位置匹配,search()会扫描整个string查找匹配,
match = re.match(pattern,'hello world!')
if match:
# 使用Match获得分组信息
print match.group()
else:print "NO"
### 输出 ###
# world
match = re.search(pattern,'hello world!')
if match:
# 使用Match获得分组信息
print match.group()
else:print "NO" match 和 saerch 区别
3)re.split(pattern, string[, maxsplit])
以数字(可以多个数字)为分割 ,maxsplit=2 表示分割两次(不是两部分)指的是用“数字”分割2次(这里有4个数字)
pattern = re.compile(r'\d+')
print re.split(pattern,'one1two2three3four4') ### 输出 ###
# ['one', 'two', 'three', 'four', ''] pattern = re.compile(r'\d+')
print re.split(pattern,'one1two2three3four4',) ### 输出 ###
# ['one', 'two', 'threefour'] re.split(pattern, string[, maxsplit])
(4)re.findall(pattern, string[, flags])见名思议 find all match 找出所有匹配的,这个应该是很常用的语句吧~
pattern = re.compile(r'\W+')
print re.findall(pattern, '/one1two*2three3four4!') ### 输出 ###
# ['/', '*', '!']
\w:代表字母数字下划线.
(6)re.sub(pattern, repl, string[, count])
对匹配的文本 用repl 替换 这里的\2(\id)代表的是第二个分组 0分组是整个文本!!!第一个左括号是第一个分组
pattern = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!'<br>你看这里的第一个分组就是i<br>第二个分组是say print re.sub(pattern,r'\2 \1', s)<br>用say i替换i say<br><br>
在这个例子里的 func函数用的挺巧妙
.title是使得第一字母大写?~~?!
import re pattern = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!' print re.sub(pattern,r'\2 \1', s) def func(m):
return m.group()<span style="color: #ff6600;">.title()</span> + ' ' + m.group().title() print re.sub(pattern,func, s) ### output ###
# say i, world hello!
# I Say, Hello World!
re.subn(pattern, repl, string[, count])
只是输出结果中有一个替换次数
输出结果对比
say i, world hello!
I Say, Hello World!
('say i, world hello!', 2)
('I Say, Hello World!', 2)
phython正则表达式 Python Re模块的更多相关文章
- python正则表达式 Python Re模块
最近在学python 练习的时候随手写的,方便以后自己参考~如果能对其他同学有所帮助就再好不过了 希望大家指正哦~ 我会随时整理的,先这样~ 正则表达式 1.元字符([ ]),它用来指定一个char ...
- Python之正则表达式(re模块)
本节内容 re模块介绍 使用re模块的步骤 re模块简单应用示例 关于匹配对象的说明 说说正则表达式字符串前的r前缀 re模块综合应用实例 正则表达式(Regluar Expressions)又称规则 ...
- python常用模块(1):collections模块和re模块(正则表达式详解)
从今天开始我们就要开始学习python的模块,今天先介绍两个常用模块collections和re模块.还有非常重要的正则表达式,今天学习的正则表达式需要记忆的东西非常多,希望大家可以认真记忆.按常理来 ...
- 【转】Python之正则表达式(re模块)
[转]Python之正则表达式(re模块) 本节内容 re模块介绍 使用re模块的步骤 re模块简单应用示例 关于匹配对象的说明 说说正则表达式字符串前的r前缀 re模块综合应用实例 参考文档 提示: ...
- 【Python爬虫】正则表达式与re模块
正则表达式与re模块 阅读目录 在线正则表达式测试 常见匹配模式 re.match re.search re.findall re.compile 实战练习 在线正则表达式测试 http://tool ...
- Python正则表达式与re模块
在线正则表达式测试 http://tool.oschina.net/regex/ 常见匹配模式 模式 描述 \w 匹配字母数字及下划线 \W 匹配非字母数字下划线 \s 匹配任意空白字符,等价于 [\ ...
- Python与正则表达式[0] -> re 模块的正则表达式匹配
正则表达式 / Regular Expression 目录 正则表达式模式 re 模块简介 使用正则表达式进行匹配 正则表达式RE(Regular Expression, Regexp, Regex) ...
- python 正则表达式re使用模块(match()、search()和compile())
摘录 python核心编程 python的re模块允许多线程共享一个已编译的正则表达式对象,也支持命名子组.下表是常见的正则表达式属性: 函数/方法 描述 仅仅是re模块函数 compile(patt ...
- python正则表达式之re模块方法介绍
python正则表达式之re模块其他方法 1:search(pattern,string,flags=0) 在一个字符串中查找匹配 2:findall(pattern,string,flags=0) ...
随机推荐
- 封装 RabbitMQ.NET
这篇文章内容会很短,主要是想给大家分享下我最近在做一个简单的rabbitmq客户端类库的封装的经验总结,说是简单其实一点都不简单.为了节省时间我主要按照Library的执行顺序来介绍,在你看来这里仅仅 ...
- Java集合整体框架
Java中的集合类有List.Set.Map Collection的实现类:List.Set List的实现类:ArrayList.LinkedList.Vector Set的实现类:HashSet. ...
- [剑指Offer] 3.从尾到头打印链表
题目描述 输入一个链表,从尾到头打印链表每个节点的值. [思路]用一个vector存储,遍历链表时每次从前面插入 /** * struct ListNode { * int val; * struct ...
- 【bzoj1787】[Ahoi2008]Meet 紧急集合 倍增LCA
题目描述 输入 输出 样例输入 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 2 4 4 6 6 6 样例输出 5 2 2 5 4 1 6 0 题解 倍增LCA 首先有集合点 ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- [SCOI2012]喵星球上的点名——堪称十种方法做的题
题意: 给你N个串对,M个询问串,对每个询问串求是多少串对的子串(在串对的某一个中作为子串),以及每个串对最终是包含了多少询问串 方法众多.. 可谓字符串家族八仙过海各显神通. 复杂度不尽相同,O(n ...
- [链接] Linux下常见的~/.bashrc、/etc/profile、/etc/ld.so.config小科普以及caffe编译遇到的相关问题解决
由于博主设置禁止转载,这里贴一个链接,http://blog.csdn.net/u014266895/article/details/61928602,内容很有用,linux下很多软件问题都是各种路径 ...
- init_connect基本用法
服务器为每个连接的客户端执行的字符串.字符串由一个或多个SQL语句组成.要想指定多个语句,用分号间隔开.例如,每个客户端开始时默认启用autocommit模式.没有全局服务器变量可以规定autocom ...
- poj 1523 割点 tarjan
Description Consider the two networks shown below. Assuming that data moves around these networks on ...
- [codeforces gym Matrix God]随机矩阵乘法
题目链接:http://codeforces.com/gym/101341/problem/I 随机真是一个神奇的方法.原本矩阵乘法是n^3的复杂度,但是这个题是让判断两个矩阵是否相等,只需要在两个矩 ...