python22 正则表达式

(1)普通字符作为原子

import re  #re专门做正则的模块
#(1)普通字符作为原子
a='bai'
str1='www.baidu.com'
re1=re.search(a,str1)  #search匹配出来的数据可以看见它的具体位置
re2=re.match(a,str1)   #match从开头匹配,若从开始就没有,则为空,只在开头匹配
re3=re.compile(str1)   #返回本身一个str1对象,用于后期做匹配(重点,做爬虫用)
re4=re.findall(a,str1) #将匹配出来的数据以列表的形式返回,而且可以多匹配(重要)
re5=re.split(a,str1)   #以匹配的a,最为分割位置,做分割,并以列表的形式返回(重要)
print(re1)
print(re2)
print(re3)
print(re4)
print(re5)

(2)非打印字符最为原子(\n)

#(2)非打印字符最为原子(\n)
import re
b='\n'
str2="""www.baidu.com
        www.taobao.com
        www.jindong.com"""
re6=re.search(b,str2)   #只匹配一个
re7=re.findall(b,str2)  #匹配所有
print(re6)
print(re7)

(3)通用字符作为原子

import re
c='\w\dpython'    #a1python,11python,_1python
d='\w\w.py.*'     #.*
str3='abcdefg351python'
re8=re.search(c,str3)
re9=re.findall(c,str3)
re10=re.findall(d,str3)
print(re8)
print(re9)
print(re10)

(4)原子表

#(4)原子表
import re
e='\w\dpython[^zxy]\w'  #不在[]中的字符,都匹配
str4='abcdefg123python_py'
re11=re.findall(e,str4)
f='\w\dpython(.*?)x'  #先匹配f,但返回括号里的(重要)
str5='abcdefg123python12a3xpye'
print(re11)

猜你喜欢

转载自blog.csdn.net/qq_17745977/article/details/89323861