'''
正则表达式对象的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. 02_Nginx基本配置与参数说明 + 辅助命令

     Nginx基本配置与参数说明,下面是nginx.conf配置文件 #运行用户 #user  nobody; worker_processes  2; #全局错误日志及PID文件 #error_l ...

  2. 【Java编程】Java中的大整数计算

    在上一篇文章中,我们实现了c语言中的大整数的运算,并且用Miller-Rabin算法实现了对大素数的测试.本来我准备用Java代码实现大整数的运算,查了一下资料发现Java中java.math的Big ...

  3. HTML中的javascript交互

    在Android开发中,越来越多的商业项目使用了Android原生控件与WebView进行混合开发,当然不仅仅就是显示一个WebView那么简单,有时候还需要本地Java代码与HTML中的javasc ...

  4. SharePoint WebPart 简单的读取列表内容的web部件

    最近,自己也在学习写一些SharePoint的部件,也就是使用对象模型,下面,介绍一下自己刚刚写的小测试程序,不足之处,还请指正. 1.  新建项目 Vs2008 – 新建 – 项目 – 类库 – 输 ...

  5. SharePoint JS感悟-js脚本

    最近有些迷恋js脚本,因为自己对Asp.net不够熟悉,又是Moss的一年级新生,不是对代码开发不感兴趣,面向对象自己也了解一些,代码也能大致读懂,个人觉得还是经验积累,作为代码开发人员,还是需要3- ...

  6. unix重定向标记

    stdin ,0,< << stdout,1,> >> stderr,2,2> 2>> 将stdout和stderr输出到同一个文件: > ...

  7. mybatis ----数据级联查询(多对一)

    工程的目录结构: 有两个表,一个文章表article ,一个用户表user. create table article (id int(11) not null auto_increment, use ...

  8. 存储引擎-Buffered tree

    Buffered-tree 也称为COLA,即cache-oblivious,可以不需要知道具体内存大小和一个块的大小,使用一套逻辑进行处理,因此内存大小可知,内存可能被临时占用去做其它事情. Buf ...

  9. asp.net core中写入自定义中间件

    首先要明确什么是中间件?微软官方解释:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?tabs=aspnet ...

  10. .Net C# 串口 Serialort safe handle has been closed 问题的解决

    最近在一个平台上使用SerialPort类开发程序时,发现程序在使用SerialPort类时会异常退出,而且诡异的是,就算把所有操作串口的代码都放在try{}catch{}块中也无法捕获这个异常.最终 ...