Python_正则表达式二
'''
正则表达式对象的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_正则表达式二的更多相关文章
- [.net 面向对象程序设计进阶] (3) 正则表达式 (二) 高级应用
[.net 面向对象程序设计进阶] (2) 正则表达式 (二) 高级应用 上一节我们说到了C#使用正则表达式的几种方法(Replace,Match,Matches,IsMatch,Split等),还 ...
- java基础---->java中正则表达式二
跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...
- 第五篇、javascript正则表达式二
一.内容概要 1)创建着呢规则表达式对象的两种方法 2)正则表达式的常用属性和方法 3)string对象常用方法中可以使用正则表达式 4)ES中其他预定义的对象:Math.Date.Number.Bo ...
- Python for Informatics 第11章 正则表达式二(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.1 正则表达式的字符匹配 ...
- javascript 正则表达式(二)
/* 正则表达式方法:test(),exec(),String对象方法:match(),search(),replace(),split() 1.test()方法: 用法: regexp对象实例.t ...
- python中关于正则表达式二
2.2 反向引用 \1, \2... 表达式在匹配时,表达式引擎会将小括号 "( )" 包含的表达式所匹配到的字符串记录下来.在获取匹配结果的时候,小括号包含的表达式所匹配到的字符 ...
- javascript正则表达式(二)——方法
正则表达式规则见:http://www.cnblogs.com/wishyouhappy/p/3756812.html,下面说明相关方法 String相关方法 概括: search() replace ...
- python_正则表达式
re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. 函数语法: \[re.match(pattern, strin ...
- Python_正则表达式一
''' 常用的正则表达式元字符 . 匹配换行符以外的任意单个字符 * 匹配位于'*'之前的字符或子模的0次或多次出现 + 匹配位于'+'之前的字符或子模式的1次或多次出现 - 用在[]之内用来表示范围 ...
随机推荐
- android加载大图,防止oom
高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...
- Xcode调试非异常导致崩溃的程序
如果App不是因为一个异常而崩溃,Xcode可能任然会指向main()函数为出错位置. 在这种情况下,你可能遇上了更低级别的问题.也许是一个除以0错误或是缓冲溢出问题,或者你寻址一个已经被释放的对象. ...
- Swift的基础之关于“!”和“?”的使用介绍
swift编程,不外乎是定义属性或者函数(方法),访问属性或者调用函数,类型转换,?和!在这几个过程中,都有一展身手的时候,而且,每次要考虑使用的时候,它们俩都会一起出现在我们的大脑中,用还是不用,如 ...
- 好看的dialog,sweet Alert Dialog 导入Android Studio
系统自带的dialog实在是丑到无法忍受.所以找到了一款比较好的第三方dialog. github 地址如下:https://github.com/pedant/sweet-alert-dialog ...
- Android遍历获取指定目录的文件
我们经常遇到一个问题,需要获取指定目录的某些扩展名的文件,并将其存在Vector中,怎么来实现呢? // 获取当前目录下所有的mp4文件 public static Vector<String& ...
- 【Visual C++】游戏编程学习笔记之五:单一背景滚动
本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44224963 作者:ZeeCod ...
- RubyGem默认源安装太慢,修改国内淘宝源
原帖地址:http://www.hiceon.com/topic/Ruby-Gem-install-source-taobao/ WHY? 由于国内网络原因(你懂的),导致 rubygems.org ...
- PHP变量的定义与相应的数据类型
在PHP中,变量的定义和C语言定义的方法是类似的,但是在PHP中,变量使用起来就非常灵活,一个变量既可以做整型,也可以是浮点型,也可以是字符串或者字符类型,通通只要在变量名前面加一个$然后加上你的变量 ...
- shim & polyfill
在JavaScript中,经常提到shim和polyfill,polyfill是shim的一种.shim 是将不同 api 封装成一种,比如 jQuery 的 $.ajax 封装了 XMLHttpRe ...
- jQuery鼠标移入移出(冒泡版和无冒泡版)
带冒泡事件的鼠标移入移出(默认的):mouseover和mouseout事件 没有冒泡事件的鼠标移入移出:mouseenter和mouseleave事件