#!/usr/bin/env python3 import re with open("conf", 'r') as f: num = '5' # reStr = re.compile(r'(?<![.d])(?:d{1,3}.){3}d{1,3}(?![.d])') re_Str = '(?<![.d])(?:d{1,3}.){3}d{1,3}(?![.d])' re_Str1 = '([a-z]+)_[0-9]' for line in f.readlines(): line = re.sub(re_Str, '192.168.1.2',line) line = re.sub(re_Str1, 'windows_'+num, line) print(line ,end='') ~