30-Python3 正则表达式
30-Python3 正则表达式
'''
re.match函数
'''
import re
print(re.match('www','www.runoob.com').span())
print(re.match('ww','www.runoob.com').span())
print(re.match('w','www.runoob.com').span())
print(re.match('com','www.runoob.com')) line = 'Cats are smarter than dogs'
macthObj = re.match(r'(.*)are(.*?).*',line,re.M|re.I)
if macthObj:
print('matchObj.group():',macthObj.group())
print('matchObj.group(1):',macthObj.group(1))
print('matchObj.group(2):',macthObj.group(2))
else:
print('No match') '''
re.search方法
'''
import re
print(re.search('www','www.runoob.com').span())
print(re.search('com','www.runoob.com').span()) line1 = 'Cats are smarter than dogs'
searchObj = re.search(r'(.*)are(.*?).*',line1,re.M|re.I)
if searchObj:
print('searchObj.group():',searchObj.group())
print('searchObj.group(1):',searchObj.group(1))
print('searchObj.group(2):',searchObj.group(2))
else:
print('Nothing found!') '''
re.match和re.search的区别
'''
line2 = 'Cats are smarter than dogs'
matchObj = re.match(r'dogs',line2,re.M|re.I)
if matchObj:
print('re.match:',matchObj.group())
else:
print('no match1')
matchObj = re.search(r'dogs',line2,re.M|re.I)
if matchObj:
print('re.search:',matchObj.group())
else:
print('no match2')
'''
检索和替换
'''
phone = '2004-959-559 #这是一个电话号码'
##删除注释
num = re.sub(r'#.*$','',phone)
print('电话号码1:',num)
##移除非数字的内容
num = re.sub(r'\D','',phone)
print('电话号码2:',num) '''
repl参数是一个函数
''' #将匹配到到数字乘以2
def double(matched):
value = int(matched.group('value'))
return str(value*2) s = 'QAA342RFDFD56FGFG'
print(re.sub('(?P<value>\d+)',double,s)) '''
compile函数
'''
pattern1 = re.compile(r'\d+')
m = pattern1.match('one12twothree34four')
m1 = pattern1.search('one12twothree34four')
print('m',m)
print('m1',m1) m2 =pattern1.match('one12twothree34four',2,10)
print('m2',m2) m3 =pattern1.match('one12twothree34four',3,10)
print('m3:',m3) print('m3.group():',m3.group())
print('m3.start():',m3.start())
print('m3.end():',m3.end())
print('m3.span():',m3.span()) pattern2 = re.compile(r'([a-z]+)([a-z])',re.I) #re.I 表示忽略大小写
mm = pattern2.match('Hello World Wide Web')
print('mm:',mm) print('mm.group(0):',mm.group(0))
print('mm.span(0):',mm.span(0))
print('mm.group(1):',mm.group(1))
print('mm.span(1):',mm.span(1))
print('mm.group(2):',mm.group(2))
print('mm.span(2):',mm.span(2))
print('mm.groups():',mm.groups())
# print('mm.group(3):',mm.group(3)) '''
findall
'''
# 在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
# 注意: match 和 search 是匹配一次 findall 匹配所有。
# 语法格式为:
# findall(string[, pos[, endpos]])
pattern3 = re.compile(r'\d+')
result1 = pattern3.findall('runoob 123 google 456')
result2 = pattern3.findall('run88oob123google456',0,10)
print('result1:',result1)
print('result2:',result2) '''
re.finditer:找到正则表达式所匹配的所有子串,并把他们作为一个迭代器返回
'''
it = re.finditer(r'\d+','qaz12edc34edc4rfv56')
for match in it:
print(match.group())
'''
re.split
'''
print('1:',re.split('\W+','runoob,runoob,runoob.')) print('2:',re.split('(\W+)','runoob,runoob,runoob.'))
print('',re.split('\W+','runoob,runoob,runoob.',1)) '''
正则表达式对象
''' '''
正则表达式修饰符-可选标志
''' '''
正则表达式模式
''' '''
正则表达式实例
'''
30-Python3 正则表达式的更多相关文章
- 详解 Python3 正则表达式(五)
上一篇:详解 Python3 正则表达式(四) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 非捕获组和命名 ...
- 详解 Python3 正则表达式(四)
上一篇:详解 Python3 正则表达式(三) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 更多强大的功能 ...
- 详解 Python3 正则表达式(三)
上一篇:详解 Python3 正则表达式(二) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 模块级别的函数 ...
- 详解 Python3 正则表达式(二)
上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...
- 详解 Python3 正则表达式(一)
本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...
- python025 Python3 正则表达式
Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...
- Python3 正则表达式
字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...
- python3 正则表达式学习笔记
re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. ~匹配成功re.match方法返回一个匹配的对象,否则返回No ...
- Python3正则表达式
正则表达式是一个特殊的字符序列,他能帮助你方便的检查一个字符串是否与某种模式匹配. re.match函数 re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,matc ...
- python3正则表达式总结
转自csdn,去这里看更多吧: https://blog.csdn.net/weixin_40136018/article/details/81183504 还有一个废话很多的详细系列,在这里:htt ...
随机推荐
- B - Pie
My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N ...
- go 的文件处理
准备一个文件 imooc.txt hello world! 一.使用 io/ioutil 包 定义一个 check 函数 func check(err error) { if err != nil { ...
- windows对象 document对象 的一些操作 9.23
函数: 四要素 1.返回类型2.函数名3.参数列表4.函数体 window . 对象 opener 打开当前窗口的源窗口 alert(window.opener); open( ) 例子: ...
- VC++、Win32 SDK、MFC的区别
这是一个初进行开发人员都可能遇到过的概念不清的问题,自己当年也同样有过误解,做技术我感觉一定要专,但是,不代表毫不关心相关的知识,至少概念层次上要知道,所以,这里还是再把这些内容纪录下来,好记性不如烂 ...
- Python学习笔记之--我又开始学习Python了(随时更新)
2019.02.09 更新 Python 学习计划已经开始几天了,跟着一本叫<Django for beginner>的书籍在学习用Django搭建自己的第一个网站,目前已经进行到第三章, ...
- java 验证字符串是否包含中文字符
中文的模式:"[\\u4e00-\\u9fa5]|\\\\u" 例子: private static final Pattern p = Pattern.compile(" ...
- SQL之层次查询
层次查询是一种确定数据行间关系的一种操作手段.层次查询遍历的是一个树形结构.基本语法如下,以下语法嵌入到标准SQL中即可达到层次查询的目的: level,... ...[注释:伪列,用于select子 ...
- iOS ARC编译器规则和内存管理规则
iOS 开发当中,自动引用计数已经是标准的内存管理方案.除了一些老旧的项目或者库已经没有人使用手动来管理内存了吧. ARC无疑是把开发者从繁琐的保留/释放引用对象逻辑中解脱出来.但这并不是万事大吉了, ...
- [模式匹配] AC 自动机 模式匹配
广义的模式匹配: https://en.wikipedia.org/wiki/Pattern_matching 字符串模式匹配: https://en.wikipedia.org/wiki/Strin ...
- airflow docker
https://github.com/puckel/docker-airflow 镜像介绍:https://hub.docker.com/r/puckel/docker-airflow/ docker ...