'''
正则表达式对象的sub(repl,string[,count=0])和subn(repl,string[,count=0])方法用来实现字符串替换功能
'''
example='''Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better tha complex.
Complext is better than nested.
Sparse is better than dense.
Readability counts.
'''
pattern = re.compile(r'\bb\w*\b',re.I) #正则表达式对象,匹配以b或B开头的单词
print(pattern.sub('*',example)) #将符合条件的单词替换为*
# * is * than ugly.
# Explicit is * than implicit.
# Simple is * tha complex.
# Complext is * than nested.
# Sparse is * than dense.
# Readability counts.
print(pattern.sub('*',example,1)) #只替换1次
# * is better than ugly.
# Explicit is better than implicit.
# Simple is better tha complex.
# Complext is better than nested.
# Sparse is better than dense.
# Readability counts.
print(re.compile(r'\bb\w*\b')) #匹配以字母b开头的单词
print(pattern.sub('*',example,1)) #将符合条件的单词替换为*,只替换1次
# * is better than ugly.
# Explicit is better than implicit.
# Simple is better tha complex.
# Complext is better than nested.
# Sparse is better than dense.
# Readability counts.
'''
正则表达式对象呢的split(strign[,maxsplit = 0])方法用来实现字符串分隔.
'''
example = r'one,two,three.four/five\six?seven[eight]nine|ten'
pattern = re.compile(r'[,./\\?[\]\|]') #指定多个可能的分隔符
print(pattern.split(example))
# ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
example = r'one1two2three3four4five5six6seven7enght8nine9ten'
pattern=re.compile(r'\d+')
print(pattern.split(example))
# ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'enght', 'nine', 'ten']
example = r'one two three four,five.six.seven,enght,nine9ten'
pattern=re.compile(r'[\s,.\d]+') #允许分隔符重复
print(pattern.split(example))
['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'enght', 'nine', 'ten'] '''
match对象:
正则表达式模块或正则表达式对象的match()方能发和search()方法匹配成功后都会返回math()对象。match对象的主要方法有grou()(返回匹配的
一个或多个子模式内容)、groups()(返回一个包含匹配的所有子模式内容的元组)、groupdict()(返回包含匹配的所有命名子模式内容字典)、start()
(返回指定子模式内容的起始位置)、end()(返回指定子模式内容的结束位置的前一个位置)、span()(返回一个包含指定子模式内容起始位置和结束前一个位置
的元组)等。下面的代码使用几种不同的方法来删除字符串中指定的内容:
'''
email='tony@tiremove_thisger.net'
m=re.search('remove_this',email) #使用search()方法返回的match对象
print(email[:m.start()]+email[m.end()]) #字符串切片
print(re.sub('remove_this','',email)) #直接使用re模块的sub()方法
# tony@tiger.net
print(email.replace('remove_this','')) #也可以直接使用字符串替换方法
# tony@tiger.net m=re.match(r"(\w+)(\w+)","Isaac Newton,physicist")
print(m.group(0)) #返回整个模式内容
# Isaac
print(m.group(1)) #返回第一个子模式内容
# Isaa
print(m.group(2))
# c
print(m.group(1,2))
# ('Isaa', 'c') '''
下面的代码演示了子模式扩展语法的用法
'''
m=re.match(r"(?P<first_name>\w+)(?P<last_name>\w+)","Malcolm Reynolds")
print(m.group('first_name')) #使用命名的子模式
# Malcolm
print(m.group('last_name'))
# m
m=re.match(r'(\d+)\.(\d+)','24.1632')
print(m.groups()) #返回所有匹配的子模式(不包括第0个)
# ('24', '1632')
m=re.match(r'(?P<first_name>\w+)(?P<last_name>\w+)','Malcolm Reynolds')
print(m.groupdict()) #以字典形式返回匹配的结果
# {'first_name': 'Malcol', 'last_name': 'm'}
exampleString = '''There should be one-and preferably only one-obvious way to do it.
Although that way may not be obvioud at first unless you're Dutch.
Now is better than never.
Athought never is often better than right now.
'''
pattern =re.compile(r'(?<=\w\s)never(?=\s\w)') #查找不在橘子开头和结尾的never
matchResult = pattern.search(exampleString)
print(matchResult.span())
# (168, 173)
pattern =re.compile(r'(?<=\w\s)never') #查找位于句子末尾的单词
mathResult=pattern.search(exampleString)
print(mathResult.span())
# (152, 157) pattern=re.compile(r'(?:is\s)better(\sthan)') #查找前面是is的better than组合
matchResult=pattern.search(exampleString)
print(matchResult.span())
# (137, 151)
print(matchResult.group(0))
# is better than
print(matchResult.group(1))
# than
pattern=re.compile(r'\b(?i)n\w+\b') #查找以n或N字母开头的所有单词
index=0
while True:
matchResult=pattern.search(exampleString,index)
if not matchResult:
break
print(matchResult.group(0),':',matchResult.span(0))
index=matchResult.end(0)
# not : (88, 91)
# Now : (133, 136)
# never : (152, 157)
# never : (168, 173)
# now : (201, 204)
pattern=re.compile(r'(?<!not\s)be\b') #查找前面没有单词not的单词be
index=0
while True:
matchResult=pattern.search(exampleString,index)
if not matchResult:
break
print(matchResult.group(0),':',matchResult.span(0))
index=matchResult.end(0)
# be : (13, 15)
print(exampleString[13:20] ) #验证一下结果是否准确
# be one-
pattern=re.compile(r'(\b\w*)(?P<f>\w+)(?P=f)\w*\b') #匹配有连续想念痛字母的单词
index = 0
while True:
matchResult=pattern.search(exampleString,index)
if not matchResult:
break
print(matchResult.group(0),':',matchResult.group(2))
index=matchResult.end(0)+1
# unless : s
# better : t
# better : t
print(s)
# aaa bb c d e fff
p=re.compile(r'(\b\w*(?P<f>\w+)(?P=f)\w*\b)')
print(p.findall(s))
[('aaa', 'a'), ('bb', 'b'), ('fff', 'f')]

Python_正则表达式二的更多相关文章

  1. [.net 面向对象程序设计进阶] (3) 正则表达式 (二) 高级应用

    [.net 面向对象程序设计进阶] (2) 正则表达式 (二)  高级应用 上一节我们说到了C#使用正则表达式的几种方法(Replace,Match,Matches,IsMatch,Split等),还 ...

  2. java基础---->java中正则表达式二

    跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...

  3. 第五篇、javascript正则表达式二

    一.内容概要 1)创建着呢规则表达式对象的两种方法 2)正则表达式的常用属性和方法 3)string对象常用方法中可以使用正则表达式 4)ES中其他预定义的对象:Math.Date.Number.Bo ...

  4. Python for Informatics 第11章 正则表达式二(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.1 正则表达式的字符匹配 ...

  5. javascript 正则表达式(二)

    /* 正则表达式方法:test(),exec(),String对象方法:match(),search(),replace(),split() 1.test()方法: 用法:  regexp对象实例.t ...

  6. python中关于正则表达式二

    2.2 反向引用 \1, \2... 表达式在匹配时,表达式引擎会将小括号 "( )" 包含的表达式所匹配到的字符串记录下来.在获取匹配结果的时候,小括号包含的表达式所匹配到的字符 ...

  7. javascript正则表达式(二)——方法

    正则表达式规则见:http://www.cnblogs.com/wishyouhappy/p/3756812.html,下面说明相关方法 String相关方法 概括: search() replace ...

  8. python_正则表达式

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

  9. Python_正则表达式一

    ''' 常用的正则表达式元字符 . 匹配换行符以外的任意单个字符 * 匹配位于'*'之前的字符或子模的0次或多次出现 + 匹配位于'+'之前的字符或子模式的1次或多次出现 - 用在[]之内用来表示范围 ...

随机推荐

  1. H5学习之旅-H5与Php交互(12)

    1.首先介绍PHP开发环境的搭建 ,在Google搜apachefriends,会有xampp的下载链接,这个工具集成了apache的很多服务 2.搭建php的编辑环境,选取eclipse安装php插 ...

  2. rpcz VC2010 构建

    rpcz VC2010 构建 rpcz 是应用ZeroMQ和Protobuf开发的RPC. 见: https://github.com/reinferio/rpcz 及 https://code.go ...

  3. Android开源项目——设置图文居中的按钮 IconButton

    本文介绍一下一个小众的开源项目--IconButton. 本文原创,转载请注明出处: http://blog.csdn.net/maosidiaoxian/article/details/435602 ...

  4. Android中让多个线程顺序执行探究

    线程调度是指按照特定机制为多个线程分配CPU的使用权. 有两种调度模型:分时调度模型和抢占式调度模型. 分时调度模型:是指让所有的线程轮流获得cpu的使用权,并且平均分配每个线程占用的CPU的时间片. ...

  5. apache 负载测试工具 ab

    1.ab工具是apache自带的工具,可以测试服务器的负载能力 2.ab工具的参数 -v:版本 -c:并发数 -n:请求数 -t: 测试所进行的最大秒数 3.例子:ab -c 100 -n 100 - ...

  6. anndroid 模糊引导界面

    先上两张图,后面补上代码 我们以前的写法是在需要显示模糊引导的地方,写一个布局,然后第一次使用的时候显示出来.但是这样做代码结构不清晰,所以我们有必要将这些View独立出来,写成一个自定义的View ...

  7. C语言实现牛顿迭代法解方程

    利用迭代算法解决问题,需要做好以下三个方面的工作: 一.确定迭代变量 在可以用迭代算法解决的问题中,我们可以确定至少存在一个可直接或间接地不断由旧值递推出新值的变量,这个变量就是迭代变量. 二.建立迭 ...

  8. LeetCode之“动态规划”:Triangle

    题目链接 题目要求: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to ...

  9. Java学习网站大全

    Java学习网站 http://www-900.ibm.com/developerWorks/cn/java/index.shtml IBM的JAVA专题--永远的蓝色巨人 http://www.hu ...

  10. 【50】java 匿名内部类剖析

    匿名内部类介绍: 匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 匿名内部类的声明 ...