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 正则表达式的更多相关文章

  1. 详解 Python3 正则表达式(五)

    上一篇:详解 Python3 正则表达式(四) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 非捕获组和命名 ...

  2. 详解 Python3 正则表达式(四)

    上一篇:详解 Python3 正则表达式(三) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 更多强大的功能 ...

  3. 详解 Python3 正则表达式(三)

    上一篇:详解 Python3 正则表达式(二) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 模块级别的函数 ...

  4. 详解 Python3 正则表达式(二)

    上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...

  5. 详解 Python3 正则表达式(一)

    本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...

  6. python025 Python3 正则表达式

    Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...

  7. Python3 正则表达式

    字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...

  8. python3 正则表达式学习笔记

    re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. ~匹配成功re.match方法返回一个匹配的对象,否则返回No ...

  9. Python3正则表达式

    正则表达式是一个特殊的字符序列,他能帮助你方便的检查一个字符串是否与某种模式匹配.   re.match函数 re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,matc ...

  10. python3正则表达式总结

    转自csdn,去这里看更多吧: https://blog.csdn.net/weixin_40136018/article/details/81183504 还有一个废话很多的详细系列,在这里:htt ...

随机推荐

  1. ubuntu如何设置开机启动默认命令行界面

    图形模式下,首先进入终端: 1. 运行 sudo vi/etc/default/grub 2. 找到 GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash” 3.改为 GR ...

  2. (domain)域名协议

    https://jingyan.baidu.com/article/2c8c281df0afd00008252aa7.html

  3. db2命令参数with ur

    查询DB2数据库,老遇到select * from XXX with ur, 好奇ur是什么作用(转) DB2中,共有四种隔离级:RS,RR,CS,UR,DB2提供了这4种不同的保护级别来隔离数据. ...

  4. 转载:mybatis踩坑之——foreach循环嵌套if判断

    转载自:作者:超人有点忙链接:https://www.jianshu.com/p/1ee41604b5da來源:简书 今天在修改别人的代码bug时,有一个需求是在做导出excel功能时,mybatis ...

  5. centos7安装Apache

    1.下载安装包wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.37.tar.gz 2.解压tar zxvf httpd-2.4.37.ta ...

  6. inotifywait实现目录监控--http://man.linuxde.net/inotifywait

    sudo apt install inotify-tools while inotifywait -q -r -e create,delete,modify,move,attrib --exclude ...

  7. cocos2dx 粒子系统

    参考文献: 1.http://blog.csdn.net/aa4790139/article/details/8126525 2.https://code.google.com/p/cocos2d-w ...

  8. JNI 入门

    1.http://cherishlc.iteye.com/blog/1756762 Android 学习笔记--利用JNI技术在Android中调用.调试C++代码 2.http://my.eoe.c ...

  9. Appium入门(8)__控件定位

    部分摘自:http://www.testclass.net/appium/appium-base-find-element/ appium 通过 uiautomatorviewer.bat 工具来查看 ...

  10. nginx最基本操作

    1.安装 yum install nginx 2.查看配置位置 nginx -t 3.查看nginx.conf,找到默认html配置路径 vi /etc/nginx/nginx.conf cd /us ...