# Python正则表达式re模块语法概述
import re
word="I am Chinese and i love my contry!"
#匹配字符串
patten=(r'i ')
print(re.search(patten,word,re.I)) #全局搜索“i ”,直到匹配到第一个为止并且返回其位置元组
print(re.match(patten,word,re.I)) #从左往右匹配“i ”,若第一个匹配成功则返回其位置元组,否则返回None
print(re.findall(patten,word,re.I)) #全局搜索“i ”,且把所有匹配到的结果返回到一个列表里,否则返回空列表
print("共出现过:",str(len(re.findall(patten,word,re.I)))+"次") #计算“i ”出现的次数
#替换字符串
#用"@*@"替换“i ”并输出,替换2次,不区分大小写
newword1=re.sub(patten,"@*@",word,2,re.I) #语法:re.sub(替换模式字符串,替换字符,原字符串,替换次数,替换方式)
print(newword1)
#分割字符串
import re
word="I am Chinese and i love my contry!"
patten2=r'and' #and为分隔符
newword2=re.split(patten2,word)#语法:re.sub(以什么字符串进行分割,在哪分割,最大分割次数[默认最大值],匹配方式)
print(newword2)
#原料compile
a=re.compile(r'\d')
na=a.search('fdgfdg81f gdfg8adfga0fdgaf6afga4a')
b=a.match('12dgfdg81f gdfg8adfga0fdgaf6afga4a')
c=a.findall('fdgfdg81f gdfg8adfga0fdgaf6afga4a')
d=a.split('fdgfdg81f gdfg8adfga0fdgaf6afga4a')
e=a.sub("@",'fdgfdg81f5gdfg8adfga0fdgaf6afga4a')
print('search:',na)
print('match:',b)
print("findall:",c)
print("sprit:",d)
print("sub",e)