1.判断是否匹配:match(开头匹配)|search(全匹配)
import re
#开头匹配
if re.match(r'world','helloworld'):
print ('match succeess')
else:
print ('match fails')
#全匹配
if re.search(r'world','helloworld'):
print ('search succeess')
else:
print ('search fails')
输出:match fails | search succeess
2.返回匹配数据 findall(查找) | split(分割) | sub(替换)
print(re.findall("(\[.*?\])",'liubiao[12]#[andy]'))
print (re.split('\.', '192.168.1.1'))
print (re.sub('(one|two|three)','num', 'one word two words three words apple',1))
输出:
[’[12]’, ‘[andy]’]
[‘192’, ‘168’, ‘1’, ‘1’]
num word two words three words apple