我有一个文本文件,其条目如下:
Interface01 :
adress
192.168.0.1
next-interface:
interface02:
adress
10.123.123.214
next-interface:
interface01 :
adress
172.123.456.123
我想解析它,只得到对应于Interface01的IP地址
我用pythonre.finall
尝试过may-things,但是没有得到任何匹配的东西
但什么都不管用。在
预期结果是192.168.0.1
。在
你可以用
参见regex demo。在Python中,使用
re.search
获得第一个匹配项,因为您只想提取1个IP地址。在图案细节:
Interface01
-一个文本子字符串\s*:\s*
-a:
用0+空格括起来adress
-一个文本子字符串\s+
-1+个空格(.*)
-组1:除换行符之外的任何0+字符。在Python demo:
^{pr2}$创建一个写着“Interface01”的模式,然后跳过所有不是数字的字符,然后得到数字和点?在
结果:
^{pr2}$更新
感谢@zipa,以下是更新的regex:
结果:
相关问题 更多 >
编程相关推荐