re.sub(pattern,repl,string,[count],[flags])
pattern = pattern string
repl = represents a replacement string
string = represents a replacement search to be original string
count = maximum number represented Alternatively, optional
flags = matched control mode, optional
Import Re pattern = R & lt ' . 1 [13579] \ {D}. 9 ' String = ' Good Luck, Tel: 13,512,312,312 ' Result = the re.sub (pattern, ' 1XXXXXXXXXX ' , String) # return the result of the replacement Print (Result)
>>>>>
good luck,tel:1XXXXXXXXXX
String replacement method 1:
the INPUT = A ( " Please enter the test text: " ) b = a.replace ( " Tokyo hot " , " *** " ) c = b.replace ( " Cang teacher " , " @@@ " ) Print ( " sensitive replace the word: \ the n- " , c)
String replacement method 2:
Import Re pattern = r ' Tokyo hot | Cang teacher | Maria Ozawa ' String = ' Good Luck, here are your favorite Tokyo heat, introduced Cang teacher and Maria Ozawa ' the Result = re.sub ( pattern, ' @@@ ' , String) # returns the result of the replacement Print (result)
》》》》》》
good luck, here are your favorite @@@ introduced @@@ @@@ and
Split string: re.split (pattern, string, [count], [flags])
pattern = pattern string
string = represents the original string to match
count = maximum number represented segmentation optional
flags = matched control mode, optional