python 正则表达式

# -*- coding: utf-8 -*-


import re

#match :从字符串开头匹配字符串 , 如果配到了返回一个对象, 匹配不到返回None
res = re.match("test.", "testasDwoedHello")
print(res)

#获取 匹配的字符串
print(res.group())



#从整个文本开始搜索
sr = re.search("D.+d", "testasDwoedHello")
print(sr.group())




#返回所有
fa = re.findall("[0-9]{1,3}", "tesda123stasDwoe435dHello667Daasdd")
print(fa)



#分割
print(re.split("[0-9]","asdasd1dads222dasd21"))


#替换
print(re.sub("[0-9]","aaa","wer1azx2vff22"))

 

posted on 2017-11-13 10:06  gaizhongfeng  阅读(144)  评论(0编辑  收藏  举报