反斜杠问题

与大多数编程语言相同,正则表达式里使用”\”作为转义字符,这就可能造成反斜杠困扰。假如你需要匹配文本中的字符”\”,

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。

# 匹配如下内容:单词+空格+单词+任意字符

m = re.match(r'(\w+) (\w+)(?P<sign>.*)', 'hello world!')
三个分组
1 \w+所有字母  hello
2  \w+所有字母 world 
3 分组名字为sign  内容为任意字符的*所有(集合)

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模块的更多相关文章

  1. python正则表达式 Python Re模块

    最近在学python 练习的时候随手写的,方便以后自己参考~如果能对其他同学有所帮助就再好不过了 希望大家指正哦~  我会随时整理的,先这样~ 正则表达式 1.元字符([ ]),它用来指定一个char ...

  2. Python之正则表达式(re模块)

    本节内容 re模块介绍 使用re模块的步骤 re模块简单应用示例 关于匹配对象的说明 说说正则表达式字符串前的r前缀 re模块综合应用实例 正则表达式(Regluar Expressions)又称规则 ...

  3. python常用模块(1):collections模块和re模块(正则表达式详解)

    从今天开始我们就要开始学习python的模块,今天先介绍两个常用模块collections和re模块.还有非常重要的正则表达式,今天学习的正则表达式需要记忆的东西非常多,希望大家可以认真记忆.按常理来 ...

  4. 【转】Python之正则表达式(re模块)

    [转]Python之正则表达式(re模块) 本节内容 re模块介绍 使用re模块的步骤 re模块简单应用示例 关于匹配对象的说明 说说正则表达式字符串前的r前缀 re模块综合应用实例 参考文档 提示: ...

  5. 【Python爬虫】正则表达式与re模块

    正则表达式与re模块 阅读目录 在线正则表达式测试 常见匹配模式 re.match re.search re.findall re.compile 实战练习 在线正则表达式测试 http://tool ...

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

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

  7. Python与正则表达式[0] -> re 模块的正则表达式匹配

    正则表达式 / Regular Expression 目录 正则表达式模式 re 模块简介 使用正则表达式进行匹配 正则表达式RE(Regular Expression, Regexp, Regex) ...

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

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

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

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

随机推荐

  1. android中常见的命名及其特点详解

    Paseal命名法 Paseal命名法特点:String MyName-DelphiInt MyAge每个单词首字母大写 Camel命名法 Camel(驼峰的意思)命名法特点:String myNam ...

  2. 两个数组的交集 II [ LeetCode - 350 ]

      原题地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/description/   给定两个数组,写一个方法来计算 ...

  3. hadoop更换硬盘

    hadoop服务器更换硬盘操作步骤(datanode hadoop目录${HADOOP_HOME}/bin    日志位置:/var/log/hadoop)1.登陆服务器,切换到mapred用户,执行 ...

  4. Rem与em的简单理解

    Rem与em的简单理解 Em单位与像素px的转换 所得的像素值 = 当前元素的font-size * em的值 比如:div的font-size:12px 10em等同于120px 12*10 =12 ...

  5. struts学习笔记(四)

    一. 文件的上传: 1). 表单需要注意的 3 点 2). Struts2 的文件上传实际上使用的是 Commons FileUpload 组件, 所以需要导入 commons-fileupload- ...

  6. spoj p104 Matrix-Tree定理

    这个问题就是经典的生成树记数问题,题目为spoj p104 highway. 首先我们引入Matrix-Tree定理,由kirchhoff证明,定理的概述为,对于图G,我们定义若干个矩阵, D[G], ...

  7. Python学习笔记 - day3 - 数据类型及运算符

    Python的数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同 ...

  8. Django-views,用户认证,login_requierd()

    分别是认证,登入,注销的功能 authenticated():验证是否登录 user = authenticate(username='someone',password='somepassword' ...

  9. socket中的函数遇见EINTR的处理【转】

    转自:http://blog.chinaunix.net/uid-21501855-id-4490453.html 这几天,写服务器代码过程当中,遇见EINRT信号的问题,我是借鉴 <unp & ...

  10. pool.map的第二个参数想传入多个咋整?

    from functools import partial from multiprocessing import Pool as ThreadPool pageurls=[] if maxpage: ...