呼如林
如果您的地址始终在一行的末尾,请在该行上定位:ip_at_end = re.compile(r'(?:[0-9]{1,3}\.){3}[0-9]{1,3}$', re.MULTILINE)此正则表达式仅匹配行尾的点分四边形(4组数字,中间有点)。演示:>>> import re>>> ip_at_end = re.compile(r'(?:[0-9]{1,3}\.){3}[0-9]{1,3}$', re.MULTILINE)>>> example = '''\... Only addresses on the end of a line match: 123.241.0.15... Anything else doesn't: 124.76.67.3, even other addresses.... Anything that is less than a dotted quad also fails, so 1.1.4... does not match but 1.2.3.4... will.... '''>>> ip_at_end.findall(example)['123.241.0.15', '1.2.3.4']