python正则表达式模块
正则表达式是对字符串的最简约的规则的表述。python也有专门的正则表达式模块re.
正则表达式函数 | 释义 |
re.match() | 从头开始匹配,匹配失败返回None,匹配成功可通过group(0)返回匹配成功的字符串 |
re.search() | 扫描整个字符串,并返回第一个匹配的字符串 |
re.sub() | 对符合要求的所有子串进行替换 |
re.findall() | 以列表形式返回所有符合条件的子串 |
re.split() | 以模式作为切分符号切分字符串,并返回列表 |
re.finditer() | 找到 RE 匹配的所有子串,并把它们作为一个迭代器返回 |
re.compile() | 把那些经常使用的正则表达式编译成正则表达式对象 |
re.group() | 返回被 RE 匹配的字符串 |
re.start() | 返回匹配开始的位置 |
re.end() | 返回匹配结束的位置 |
re.span() | 返回一个元组包含匹配 (开始,结束) 的位置 |
import re list=['15865548338cong' ,'abcd18701687236ef','gafei18511371536' ,'wb15865737271dad13244556677']
print(list)
for items in list:
# start
l=re.match( r'\d{11}',items )
print('match:',l )
if l!=None:
print('match.group(0)',l.group(0))
# start-end
m = re.search( '[0-9]{11}',items )
print('search:',m.group( 0))
print('search,.start:',m.start())
print('search,.end:',m.end())
print('search,.span:',m.span())
# replace
n=re.sub( r'\d','*' ,items)
print('sub:',n )
# return list
o=re.findall(r'\d{11}',items )
print('findall:',o )
# str2list
p=re.split( r'\d+',items )
print('split:',p )
# iterator
q=re.finditer(r'\d+',items )
for i in q:
print('finditer:' ,i.group())
pattern=re.compile(r'\d{11}')
print pattern.findall(items) print('***********************************************' )
输出结果:
['15865548338cong', 'abcd18701687236ef', 'gafei18511371536', 'wb15865737271dad13244556677']
('match:', <_sre.SRE_Match object at 0x00000000025D2510>)
('match.group(0)', '')
('search:', '')
('search,.start:', 0)
('search,.end:', 11)
('search,.span:', (0, 11))
('sub:', '***********cong')
('findall:', [''])
('split:', ['', 'cong'])
('finditer:', '')
['']
***********************************************
('match:', None)
('search:', '')
('search,.start:', 4)
('search,.end:', 15)
('search,.span:', (4, 15))
('sub:', 'abcd***********ef')
('findall:', [''])
('split:', ['abcd', 'ef'])
('finditer:', '')
['']
***********************************************
('match:', None)
('search:', '')
('search,.start:', 5)
('search,.end:', 16)
('search,.span:', (5, 16))
('sub:', 'gafei***********')
('findall:', [''])
('split:', ['gafei', ''])
('finditer:', '')
['']
***********************************************
('match:', None)
('search:', '')
('search,.start:', 2)
('search,.end:', 13)
('search,.span:', (2, 13))
('sub:', 'wb***********dad***********')
('findall:', ['', ''])
('split:', ['wb', 'dad', ''])
('finditer:', '')
('finditer:', '')
['', '']
***********************************************
请按任意键继续. . .
python正则表达式模块的更多相关文章
- Python 正则表达式模块 (re) 简介
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用这一内嵌于 Python 的语言工具,尽管不能 ...
- Python正则表达式模块(re模块)
Python是我接触到的第一门编程语言,虽然它足够简单,但是对于当时刚刚接触编程语言的我来说还是有些难度的,于是只是了解了一些Python的基本语法,稍微深入一点的地方都没怎么了解.不过,到现在为止, ...
- python正则表达式模块re:正则表达式常用字符、常用可选标志位、group与groups、match、search、sub、split,findall、compile、特殊字符转义
本文内容: 正则表达式常用字符. 常用可选标志位. group与groups. match. search. sub. split findall. compile 特殊字符转义 一些现实例子 首发时 ...
- python正则表达式模块re
正则表达式的特殊元素 匹配符号 描述 '.'(点dot) 在默认模式下,它匹配除换行符之外的任何字符.如果指定了DOTALL标志,则匹配包括换行符在内的任何字符 '^'(Caret) 匹配以字符串开头 ...
- 更强大的python正则表达式模块 -- regex
python内置的正则表达库re比较弱,而且似乎在好几年里都没太大的变化. 这里介绍这个python模块regex,实现了更多方便好用的功能. 比如:\p{han} 可以匹配汉字, \p{Latin} ...
- 一篇搞定Python正则表达式
1. 正则表达式语法 1.1 字符与字符类 1 特殊字符:\.^$?+*{}[]()| 以上特殊字符要想使用字面值,必须使用\进行转义 2 字符类 1. 包含在[]中的一个或者多个字符被称为字符 ...
- Python正则表达式很难?一篇文章搞定他,不是我吹!
1. 正则表达式语法 1.1 字符与字符类 1 特殊字符:.^$?+*{}| 以上特殊字符要想使用字面值,必须使用进行转义 2 字符类 1. 包含在[]中的一个或者多个字符被称为字符类,字符类在匹配时 ...
- Python正则表达式学习记录
常用的命令: http://www.runoob.com/python/python-reg-expressions.html 使用中相关注意问题: 1. 中括号里的表示从N到M需要用横线‘-’, 而 ...
- Python正则表达式 学习笔记
python第一个正则表达式 1. import re : python正则表达式模块 2. 第一个正则表达式 re.compile(r'imooc') pattern.match('imooc py ...
随机推荐
- laravel routes除了默认路由,其他的都无效 解决方案
按照教程.该php升级到5.5,所有是开放的扩展,默认路由进入,证明代码错误,平时不开rewrite铅 假设你其它路由,localhost/文件夹/public/index.php/home能够进去. ...
- angular中通过$location获取路径(参数)的写法
以下获取与修改的 URL 以 ( http://172.16.0.88:8100/#/homePage?id=10&a=100 ) 为例 [一]获取 (不修改URL) //1.获取当前完整 ...
- WPF依赖属性(续)(3)依赖属性存储
原文:WPF依赖属性(续)(3)依赖属性存储 在之前的两篇,很多朋友参与了讨论,也说明各位对WPF/SL计数的热情,对DP系统各抒已见,当然也出现了一些分歧. 以下简称DP为依赖属性 ...
- Exclusive access control to a processing resource
A data processing system is provided with multiple processors that share a main memory. Semaphore va ...
- 微信公众平台消息接口开发(26)从Hello2BizUser文本到subscribe事件
微信公众平台 微信公众平台开发模式 消息接口 企业微信公众平台 Hello2BizUser subscribe 订阅事件 作者:方倍工作室 原文:http://www.cnblogs.com/txw1 ...
- string操作
常用的功能测试: #! -*- coding:utf-8 -*- import string s = 'Yes! This is a string' print '原字符串:' + s print ' ...
- C++继承经典样例
c++继承经典样例 #include <iostream.h> class Base { private: int b_number; public: Ba ...
- 机器学习:DeepDreaming with TensorFlow (三)
我们看到,利用TensorFlow 和训练好的Googlenet 可以生成多尺度的pattern,那些pattern看起来比起单一通道的pattern你要更好,但是有一个问题就是多尺度的pattern ...
- ASP.NET Core Razor 布局视图 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core Razor 布局视图 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Razor 布局视图 上一章节中我们学习了如何使用 EF ...
- 嵌入式开发(*(volatile unsigned long *)) 认识
一个.说明 (*(volatile unsigned long *)) 这个语句对于不同的计算机体系结构,设备可能是port映射,也可能是内存映射的. 假设系统结构支持独立的IO地址空间.而且是por ...