python re 模块小结
前言:
本人环境windows 7 64位,python2.7
re是什么:
regular expression缩写,意为正则表达式,是python的众多模块之一
re用途:
从文本中有选择的批量抽取想要的文本碎片
re类型:
分为DFA(确定的有穷状态自动机)和NFA(非确定的有穷状态自动机)
re的安装:
打开DOS;CD到pip目录下;输入命令pip install re
re常用方法:
1.re.compile(pattern, flags=0)
pattern是str类型的,例:pattern = r‘^.*?$’
2.re.findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.返回字符串中所有非重叠匹 配的列表。
例1:print re.findall(r’(s)(d)’, ‘gsd sd fsa ggh sd hf sdgf ’)
结果:[('s', 'd'), ('s', 'd'), ('s', 'd'), ('s', 'd')]
例2:print re.findall(r'(s)d','gsd sd fsa ggh sd hf sdgf')
结果:['s', 's', 's', 's']
例3:print re.findall(r'sd','gsd sd fsa ggh sd hf sdgf')
结果:['sd', 'sd', 'sd', 'sd']
用途:抽取网页源代码中的链接等
3.re.split(pattern, string, maxsplit=0, flags=0)
Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings.返回list
例:print re.split(r's','jsjkjoishioshuisguusnjshbsg')
结果:['j', 'jkjoi', 'hio', 'hui', 'guu', 'nj', 'hb', 'g']
用途:将大段文本分成易于处理的小片段
4.re.match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.返回的object类似指针
5.re.search(pattern, string, flags=0)
Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found.返回的object类似指针
参考资料:IDLE的help命令;
后记:如有错漏,欢迎指正,有时间就更新
本文是博主原创,转载请注明出处,并@我心飞翔2015,谢谢合作!
python re 模块小结的更多相关文章
- Python常用模块小结
目录 Python常用模块小结 一.Python常用模块小结 1.1 time模块 1.2 datetime模块 1.3 random模块 1.4 os模块 1.5 sys模块 1.6 json模块 ...
- python时间模块小结
1.datetime 模块 为日期和时间处理同时提供了简单和复杂的方法.支持日期和时间算法的同时,实现的重点放在更有效的处理和格式化输出.该模块还支持时区处理: 简单例子: from datetime ...
- python 时间模块小结
python有两个重要的时间模块,分别是time和datetime time模块 表示时间的几种方法 时间元组 time.struct_time( tm_year=2016, tm_mon=7, tm ...
- Python标准模块--functools
1 模块简介 functools,用于高阶函数:指那些作用于函数或者返回其它函数的函数,通常只要是可以被当做函数调用的对象就是这个模块的目标. 在Python 2.7 中具备如下方法, cmp_to_ ...
- python——常用模块
python--常用模块 1 什么是模块: 模块就是py文件 2 import time #导入时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的 ...
- Python 正则表达式模块 (re) 简介
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用这一内嵌于 Python 的语言工具,尽管不能 ...
- python常用模块之时间模块
python常用模块之时间模块 python全栈开发时间模块 上次的博客link:http://futuretechx.com/python-collections/ 接着上次的继续学习: 时间模块 ...
- 万恶之源 - Python 自定义模块
自定义模块 我们今天来学习一下自定义模块(也就是私人订制),我们要自定义模块,首先就要知道什么是模块啊 一个函数封装一个功能,比如现在有一个软件,不可能将所有程序都写入一个文件,所以咱们应该分文件,组 ...
- python常用模块collections os random sys
Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...
随机推荐
- thinkphp分页+条件查询
最近项目上面有一个带条件查询的分页列表,一开始form用的post,点击第二页就没有跳转成功,原因是分页是get请求,post数据链接到其他页面就会被清除. 解决办法: 1.form表单method= ...
- C++ 关于运算符重载
转载来源:http://c.biancheng.net/cpp/biancheng/view/216.html 重载运算符的函数一般格式如下: 函数类型 operator 运算符名称 (形参表列 ...
- LeetCode Remove Element删除元素
class Solution { public: int removeElement(int A[], int n, int elem) { ]; int i,num=n; ;i<n;i++){ ...
- World Wind Java开发之十五——加载三维模型(转)
之前的一篇博客是关于加载粗三维模型的,见http://blog.csdn.net/giser_whu/article/details/43452703,这个地方还存在着不能加载纹理的问题,一直没呢解决 ...
- AOJ 558 Cheese(bfs)
题意:网格图,老鼠吃奶酪,吃完奶酪体力值会增加,只能吃硬度不大于体力值的,问最小步数. 思路:按硬度从小到大的吃起,依次求最短路. 我用曼哈顿距离估价的A*,和普通bfs的time没区别啊,还把优先级 ...
- RobotFramework:钉钉扫码登录UI自动化
背景: 遇到一个项目,使用的是钉钉扫码登录,一时间不知道该怎么下手了,还是先F12抓包看下都有什么数据传输吧. 分析: 先熟悉下钉钉扫码登录的逻辑,参考官文:https://open-doc.ding ...
- python实现剑指offer对称的二叉树
题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. # -*- coding:utf-8 -*- # class TreeNode ...
- 【数学 随机 技巧】cf364D. Ghd
随机化选讲的例题 John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positi ...
- String&StringBuffer&StringBuilder区别
String String类是final类故不可以继承,也就意味着String引用的字符串内容是不能被修改.String有两种实例化方式: (1)直接赋值(例中,String str = &q ...
- 7- vue django restful framework 打造生鲜超市 -商品类别数据展示(上)
Vue+Django REST framework实战 搭建一个前后端分离的生鲜超市网站 Django rtf 完成 商品列表页 并没有将列表页的数据json 与前端的页面展示结合起来 讲解如果将dr ...