打印字符串替换
import re def string_match(match_keys,string): for each_key in match_keys: if each_key not in string: return False return True def repace_match_key(match_keys,string): for each_key in match_keys: string = string.replace(each_key,'') return string original_string = r' temp ("%s [%s] erro [%d] alert", __ FUNCTION __ ,__LINE__)' print(original_string.count('[%s]')) result_string = '' w = re.match(r'.+(\s{0,4}\[%d\]\s{0,4})',original_string) print(w.group(1)) n = re.match(r'.+(\s{0,4}\[%s\]\s{0,4})',original_string) print(n.group(1)) m = re.match(r'.+(,\s{0,4}__\s{0,4}FUNCTION\s{0,4}__)',original_string) print(m.group(1)) if m and n and w: function_string = m.group(1) match_keys = [n.group(1),w.group(1),function_string,r',__LINE__'] if string_match(match_keys,original_string): result_string = repace_match_key(match_keys,original_string) print(result_string)