python中关于正则表达式
>>> import re
>>> s='nihaoma'
>>> re.findall(s,'nihao')
[]
>>> re.findall(s,'nihaoma')
['nihaoma']
>>> st='ni hao ma ? wo hen hao.'
>>> res='ni'
>>> re.findall(s,res)
[]
>>> re.findall(res,s)
['ni']
>>> res=r'ni'
>>> re.findall(res,st)
['ni']
>>> res=r't[oi]p'
>>> sr='montion top tip tep'
>>> re.findall(res,sr)
['top', 'tip']
>>> res=r't[^io]p' #此处^是取反,获取t~p,中间不为i和o的字符,sr中符合条件的是tep
>>> re.findall(res,sr)
['tep']
>>> r='t[abc^]' #此处^是当一个字符
>>> re.findall(r,'t^')
['t^']
>>> res=r'^t[io]p'
>>> re.findall(res,sr)
[]
>>> res=r''
>>> sr='tep haieng'
>>> res=r'^tep' #匹配tep 字符,^表示匹配行首
>>> re.findall(res,sr)
['tep']
>>> sr='haieng tep'
>>> re.findall(res,sr)
[]
>>> res=r'tep$' #匹配tep字符,$表示匹配行尾的tep
>>> re.findall(res,sr)
['tep']
>>> te='^ab'
>>> r=r'\^ab'
>>> re.findall(r,te)
['^ab']
>>> r=r'^abc'
>>> re.findall(r,'^abc') #此处'^'是个字符,r=r'^abc'中^是表示匹配行首
[]
\
\d : 匹配任意十进制数,【0-9】 \D:匹配非数字字符,[^0-9]
\s:匹配任何空白字符 \S:匹配任何非空白字符
\w:匹配任何字母数字字符,[a-zA-Z0-9_]
\W:匹配任何 非字母数字字符,[^a-zA-Z0-9_]
>>> r=r'ab*' #*的用法,指定字符匹配0次或多次,不止是一次
>>> re.findall(r,'a') #
['a']
>>> re.findall(r,'ab')
['ab']
>>> re.findall(r,'abbbbbbbb')
['abbbbbbbb']
>>> r=r'ab+' #+的用法,指定支付匹配至少一次
>>> re.findall(r,'a')
[]
>>> re.findall(r,'ab')
['ab']
>>> re.findall(r,'abbbbb')
['abbbbb']
>>> r=r'ni?' #?匹配一次或0次
>>> re.findall(r,'niiii')
['ni']
>>> re.findall(r,'n')
['n']
>>> re.findall(r,'ni')
['ni']
>>>
>>> r=r'ab+?'
>>> re.findall(r,'abbbbbb')
['ab']
>>> re.findall(r,'a')
[]
{m,n}:至少m次重复,最多n次,{0,1}=?,{0,}=*,{1,}=+
re.compile的用法:
>>>r1=r'\d{3,4}-?\d{8}'
>>>p_tel=re.compile(r1)
>>> p_tel.findall('0101322134544')
['010132213454']
>>> r1=r'\d{3}-?\d{8}'
>>> p_tel.findall('010122134544')
['010122134544']
>>> p_tel.findall('010-1221345')
[]
>>> te=re.compile('cstv',re.I) #re.I的意思是不用区分字母大小写
>>> te.findall('cStv')
#match匹配字符串开始位置
>>> te=re.compile('ceshi',re.I)
>>> te.match('ceshi nihao')
<_sre.SRE_Match object; span=(0, 5), match='ceshi'>
>>> te.match('nihao ceshi')
>>> te.match('nihao ceshi nine')
#search查找字符串,不管位置,只要存在就查找出
>>> te.search('ceshi nihao')
<_sre.SRE_Match object; span=(0, 5), match='ceshi'>
>>> te.search('nihao ceshi')
<_sre.SRE_Match object; span=(6, 11), match='ceshi'>
>>> te.search('nihao ceshi hah')
<_sre.SRE_Match object; span=(6, 11), match='ceshi'>
Pattern的用法,Pattern不能直接实例化,必须使用re.compile()进行构造。
>>> te.pattern
'ceshi'
>>> te
re.compile('ceshi', re.IGNORECASE)
>>> te.pattern
'ceshi'
>>> te.flags #flags: 编译时用的匹配模式。数字形式。
34
>>> x=te.match('ceshi nihao')
>>> x.group()
'ceshi'

split分割的用法
>>> x=te.split('.')
>>> x
['ni', 'hao', 'shi', 'bu', 'shi']
>>> s='23+34*23-12'
>>> re.split(r'[\+\*\-]',s)
['23', '34', '23', '12']
re.S的用法匹配转移字符,比如:\n,\t等
>>> r1=r'csvt.net'
>>> re.findall(r1,'csvt\nnet',re.S)
['csvt\nnet']
>>> s="""
hello csvt
csvt hello
csvt hehe
"""
>>> s
'\nhello csvt\ncsvt hello\ncsvt hehe\n'
>>> type(s)
<class 'str'>
>>> r=r'^csvt'
>>> re.findall(r,s)
[]
>>> re.findall(r,s,re.M)
['csvt', 'csvt']
re.X的用法,正则多行的时候可用re.X
>>> tel=r"""
\d{3,4}
-?
\d{11}
"""
>>> tel
'\n\\d{3,4}\n-?\n\\d{11}\n'
>>> re.findall(tel,'0101-12345678342',re.X)
['0101-12345678342']
>>> re.findall(tel,'0101-12345678342')
[]
z
>>> r=r'\d{11}@\w{2}\.com|\d{11}@\w{2}\.cn'
>>> re.findall(r,'130465676670@qq.com')
['30465676670@qq.com']
python中关于正则表达式的更多相关文章
- Python::re 模块 -- 在Python中使用正则表达式
前言 这篇文章,并不是对正则表达式的介绍,而是对Python中如何结合re模块使用正则表达式的介绍.文章的侧重点是如何使用re模块在Python语言中使用正则表达式,对于Python表达式的语法和详细 ...
- 在Python中使用正则表达式同时匹配邮箱和电话并进行简单的分类
在Python使用正则表达式需要使用re(regular exprssion)模块,使用正则表达式的难点就在于如何写好p=re.compile(r' 正则表达式')的内容. 下面是在Python中使用 ...
- python模块 re模块与python中运用正则表达式的特点 模块知识详解
1.re模块和基础方法 2.在python中使用正则表达式的特点和问题 3.使用正则表达式的技巧 4.简单爬虫例子 一.re模块 模块引入; import re 相关知识: 1.查找: (1)find ...
- Python学习-38.Python中的正则表达式(二)
在Python中,正则表达式还有较其他编程语言有特色的地方.那就是支持松散正则表达式了. 在某些情况,正则表达式会写得十分的长,这时候,维护就成问题了.而松散正则表达式就是解决这一问题的办法. 用上一 ...
- Python学习-37.Python中的正则表达式
作为一门现代语言,正则表达式是必不可缺的,在Python中,正则表达式位于re模块. import re 这里不说正则表达式怎样去匹配,例如\d代表数字,^代表开头(也代表非,例如^a-z则不匹配任何 ...
- [Python]网络爬虫(七):Python中的正则表达式教程
转自:http://blog.csdn.net/pleasecallmewhy/article/details/8929576#t4 接下来准备用糗百做一个爬虫的小例子. 但是在这之前,先详细的整理一 ...
- [Python]网络爬虫(七):Python中的正则表达式教程(转)
接下来准备用糗百做一个爬虫的小例子. 但是在这之前,先详细的整理一下Python中的正则表达式的相关内容. 正则表达式在Python爬虫中的作用就像是老师点名时用的花名册一样,是必不可少的神兵利器. ...
- 在python中使用正则表达式(转载)
https://www.cnblogs.com/hanmk/p/9143514.html 在python中使用正则表达式(一) 在python中通过内置的re库来使用正则表达式,它提供了所有正则表 ...
- python中的正则表达式(re模块)
一.简介 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎 ...
- python中关于正则表达式三
2015年8月14日 11:10 7.2正则表达式操作 正则表达式使用反斜杠字符'\'来暗示一些特殊的形式或者允许特殊的字符使用但是没有调用它们特殊的意思.在字符串常量中的相同目标的字符的python ...
随机推荐
- [SAP ABAP开发技术总结]预定义(内置)数据类型
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- poj 1279 -- Art Gallery (半平面交)
鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- Codeforces Round #237 (Div. 2) B题模拟题
链接:http://codeforces.com/contest/404/problem/B B. Marathon time limit per test 1 second memory limit ...
- C++指针内存
这是一个关于C++指针的问题,思考了一下 void GetMemory(char *p, int num){ p = (char*) malloc (sizeof(char) * num); } vo ...
- Redis基础知识之——自定义封装单实例和普通类Redis
一.普通Redis实例化类: class MyRedis { private $redis; public function __construct($host = '121.41.88.209', ...
- 08 Transactions
本章提要------------------------------------------事务的特性事务控制语句------------------------------------------事 ...
- Linux 下操作GPIO(两种方法,驱动和mmap)(转载)
目前我所知道的在Linux下操作GPIO有两种方法: 1.编写驱动,这当然要熟悉Linux下驱动的编写方法和技巧,在驱动里可以使用ioremap函数获得GPIO物理基地址指针,然后使用这个指针根据io ...
- php PDO链接SQL SERVER
操作系统Windows7 apache 2.2 php php-5.4.45-Win32-VC9-x86 sqlserver 2008 下载SQLSRV32 https://www.microsoft ...
- 【Oracle 数据迁移】环境oracle 11gR2,exp无法导出空表的表结构【转载】
今天做数据迁移,但是发现有些空表无法exp,后来找到问题所在. [原文]:http://www.cnblogs.com/wenlong/p/3684230.html 11GR2中有个新特性,当表无数据 ...
- enum类型
1.设有变量a,b,c被说明为上述的weekday,可采用下述任一种方式: enum weekday{sun,mon,tue,wed,thu,fri,sat}; enum weekday a,b,c; ...