'''
正则表达式对象的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. Mahout canopy聚类

    Canopy 聚类 一.Canopy算法流程 Canopy 算法,流程简单,容易实现,一下是算法 (1)设样本集合为S,确定两个阈值t1和t2,且t1>t2. (2)任取一个样本点p,作为一个C ...

  2. SpriteBuilder使用Shader Effect的另一种方法

    记住你并不是必须要使用Effect节点去给一个特定的精灵应用效果. 你只要选择一个精灵然后切换至项目属性窗口(Item Properties tab),找到effects用户接口在CCSprite属性 ...

  3. Android群英传笔记——第八章:Activity与Activity调用栈分析

    Android群英传笔记--第八章:Activity与Activity调用栈分析 开篇,我们陈述一下Activity,Activity是整个应用用户交互的核心组件,了解Activity的工作模式,生命 ...

  4. Android中SQLiteOpenHelper类的onUpgrade方法浅谈

    public abstract void onUpgrade(SQLiteDatabase db,int oldVersion,int new Version) 这个方法在实现时需要重写. onUpg ...

  5. RHEL自动安装zookeeper的shell脚本

    RHEL自动安装zookeeper的shell脚本 A:本脚本运行的机器,Linux RHEL6 B,C,D,...:待安装zookeeper cluster的机器, Linux RHEL6 首先在脚 ...

  6. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  7. iOS中获取本地通讯录联系人以及汉字首字母排序

    iOS中获取手机通讯录中的联系人信息: /*** 加载本地联系人*/ - (void)loadLocalContacts { //新建一个通讯录类 ABAddressBookRef addressBo ...

  8. 开源组件photoView学习

    功能特性 支持放缩超出边界,多点触控和双击事件 滚动和滑动 和ViewPager等能完美兼容 矩阵变化等有回调,方便前台其他展示的改变 单击,长按都有回调提醒 源码剖析 那么怎么来学习他的源码呢,我们 ...

  9. 【5】-阿里面试题android网络图片加载优化

    题目: 遇到网络不好,卡顿的时候如何对网络的加载图片进行优化? 思路: 从加载的图片的本身和手机的存储两方面考虑 解决办法: 1.找现有图片格式的替换者 在众多的图片格式中,选择了Google的Web ...

  10. java Map遍历

    http://www.cnblogs.com/fczjuever/archive/2013/04/07/3005997.html 1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entr ...