pythoon爬虫(二十四) 正则表达式findall、sub、split、compile函数

findall:通过正则表达式找到字符串中所有想要得字符

text="apple's price $99,orange's price $10"
ret=re.findall('\$\d+',text)
print(ret)

 

 sub:替换字符

text="apple's price $99,orange's price $10"
ret=re.sub('\$\d+',"0",text)
print(ret)

 

 split:分割函数

text="hello world ni hao"
ret=re.split(' ',text)
print(ret)

 

 或者:ret=re.split('[^a-zA-Z] ',text)

compile:正则表达式执行很多次,效率低时使用

 

text="the number is 20.50"
r=re.compile('\d+\.?\d*')
ret=re.search(r,text)
>>20.50

 

posted on 2020-03-12 18:31  方木Fengl  阅读(342)  评论(0编辑  收藏  举报

导航