1 match = re.match('www', 'www.runoob.com') 2 print match.group(0) #匹配結果 3 print match.span() #匹配結果的起始和結束位置(0, 3)
1 re.search('com', 'www.runoob.com') #能夠匹配任意位置 2 print match.group(0) #匹配結果 3 print match.span() #匹配結果的起始和結束位置(0, 3)
1 re.sub(pattern, repl, string, count=0, flags=0) 2 參數: 3 ● pattern : 正則中的模式字符串。 4 ● repl : 替換的字符串,也可爲一個函數。 5 ● string : 要被查找替換的原始字符串。 6 ● count : 模式匹配後替換的最大次數,默認 0 表示替換全部的匹配。