import re
match方法:
用于指定文本与待匹配的字符串匹配,返回SRE_Match对象,可以用group方法获得匹配成功的字符串
a='hello'
m=re.match(a,'hello')
if m is not None:
print(m.group())
'''
#结果:hello
匹配是从头开始的如果前面的匹配成功就会返回
a='h'
m=re.match(a,'hello')
print(m.group())
#结果:h
search 方法
search方法用来搜索字符串
m=re.search('hello','hello,python')
print(m.group())
#结果:hello
#匹配多个字符串
s='bike|car|truck'
m=re.match(s,'car')
print(m.group())
字符集
#待匹配的字符串需要跟第一个字母在第一组中,第二个字母在第二组中
m=re.match('[ab][cd]','ad')
if m is not None:
print(m.group())
'''
重复集
s='a*'
strlist=['','aa','a',' ','aaa']
for i in strlist:
m=re.match(s,i)
if m is not None:
print(m.group())
#结果:
'''
aa
a
aaa
'''
注意:0个也可以匹配成功
对于至少出现一次’a’:
‘’’
s='a+'
strlist=['','aa','a',' ','aaa']
for i in strlist:
m=re.match(s,i)
if m is not None:
print(m.group())
结果:
aa
a
aaa
re.findall
功能:扫描整个字符串,并返回结果列表。
元字符
1:. 匹配出换行符以外的任意字符
2:[] 匹配方括号中包含的任意一个字符
[xob] 匹配‘x’,'o','b' 中任意一个字符
[A-Z] 匹配大写字母
[0-9] 匹配数字
[0-9a-zA-Z] 匹配任意的数字和字母
[^orb] 匹配除了orb这几个字母以外的所有字符
^称为脱字符,表示不匹配集合中的字符
[^0-9] 匹配所有的非数字字符
\d 匹配数字,效果同[0-9]
\D 匹配非数字字符,效果同[^0-9]
\w 匹配数字字母下划线,效果同[0-9a-zA-Z_]
\W 匹配非数字字母下划线,效果同[^0-9a-zA-Z_]
\s 匹配任意的空白符(空格,换行,回车,换页,制表)
\S 匹配任意的非空白符(空格,换行,回车,换页,制表)
锚字符
'''
'''