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 ...
随机推荐
- Spark RDD 默认分区数量 - repartitions和coalesce异同
RDD.getNumPartitions()方法可以获得一个RDD分区数量, 1.默认由文件读取的话,本地文件会进行shuffle,hdfs文件默认会按照dfs分片来设定. 2.计算生成后,默认会按照 ...
- spark - Locality Level
这几个值在图中代表 task 的计算节点和 task 的输入数据的节点位置关系 PROCESS_LOCAL: 数据在同一个 JVM 中,即同一个 executor 上.这是最佳数据 locality. ...
- 这些简单实用的Word技巧,你get了吗
快速选中多个对象 如果需要将某些文本设置成相同的格式,我们不需要一个个的设置,只要选中多个文本然后一起设置就可以了. 单击开始——选择编辑——选择——选择格式相似的文本 快速清除所有格式 那么当我们不 ...
- Windows Server 2008 R2远程协助选项 灰色
管理工具——〉服务器管理器——〉功能——〉添加功能 窗口中"远程服务器管理工具"下边的"远程协助"打上对钩"再点击"下一步"再 ...
- 如何选择windows 10 系统中默认打开程序
有时候我们会遇到打开某些文件需要通过open with 选择打开的应用程序,然后再点选always open with. 但是有时候这个方法不起作用,我们可以用如下方法: 1.从settings找到a ...
- int main(int argc,char *argv[])与int main(int argc,char **argv)区别?
int main(int argc,char *argv[])与int main(int argc,char **argv)区别? 这两种是一个等价的写法 而int main(int argc,cha ...
- {MySQL存储引擎介绍}一 存储引擎解释 二 MySQL存储引擎分类 三 不同存储引擎的使用
MySQL存储引擎介绍 MySQL之存储引擎 本节目录 一 存储引擎解释 二 MySQL存储引擎分类 三 不同存储引擎的使用 一 存储引擎解释 首先确定一点,存储引擎的概念是MySQL里面才有的,不是 ...
- [No0000DD]C# StringEx 扩展字符串类 类封装
using System; using System.Text.RegularExpressions; namespace Helpers { /// <summary> /// 包含常用 ...
- shell之使用paste命令按列拼接多个文件
试验文件: [root@db03 shell-script]# cat text1.txt 1 2 3 4 5 [root@db03 shell-script]# cat text2.txt orac ...
- 应用打开其xlspptdoc等
http://www.libxl.com/documentation.html xls读写编辑类库libxl https://blog.csdn.net/songbob/article/detail ...