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 代码段. 把相关的代码 ...
随机推荐
- C++ 关于运算符重载
转载来源:http://c.biancheng.net/cpp/biancheng/view/216.html 重载运算符的函数一般格式如下: 函数类型 operator 运算符名称 (形参表列 ...
- HDU3874 线段树 + 离线处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 , 线段树(或树状数组) + 离线处理 下午做了第一道离线处理的题目(HDU4417),多少有点 ...
- 工作流性能优化(敢问activiti有扩展性?)(1)
工作流待办(首页待办列表),加载缓慢,activiti本机,看了代码又是全部数据加载到内存,然后代码过滤,我为什么又说又呢? 用VisualVM做性能测试: 之前同事给的解决方案: 1.把&quo ...
- 分析ELF的加载过程
http://blog.chinaunix.net/uid-72446-id-2060538.html 对于可执行文件来说,段的加载位置是固定的,程序段表中如实反映了段的加载地址.对于共享库来?段的加 ...
- 深入理解计算机系统_3e 第十章家庭作业 CS:APP3e chapter 10 homework
10.6 1.若成功打开"foo.txt": -->1.1若成功打开"baz.txt": 输出"4\n" -->1.2若未能成功 ...
- 谷歌SwitchySharp && SwitchyOmega插件
http://pan.baidu.com/s/1jOwgu 谷歌SwitchySharp插件 http://pan.baidu.com/s/1mgl7e2k SwitchySharp的升级版Switc ...
- webpack4简单入门
安装webpack需要安装node环境,因此需要在电脑中安装node.node官网https://nodejs.org/,安装LTS版本即可. webpck基本概念 entry:分析依赖模块的入口 o ...
- 梁勇(Danniel Liang) java教材例题:java程序购买额按税率求营业税 java中数值保留2位小数的方法
package com.swift; import java.util.Scanner; public class PurchaseTaxDecimalsTwo { public static voi ...
- React报错 :browserHistory doesn't exist in react-router
由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...
- list变set去重,set交集
set 取交集 并集 删除没有的元素 不会报错 remove 会报错 https://www.cnblogs.com/alex3714/articles/5717620.html