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的用法匹配转移字符,比如: , 等

>>> r1=r'csvt.net'
>>> re.findall(r1,'csvt net',re.S)
['csvt net']

>>> s="""
hello csvt
csvt hello
csvt hehe
"""
>>> s
' hello csvt csvt hello csvt hehe '
>>> 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
' \d{3,4} -? \d{11} '
>>> 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']

原文地址:https://www.cnblogs.com/xianhaiyan/p/4781858.html