python学習(8)正規表現(reライブラリ)操作関数


python正規表現の一般的な操作関数
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