如何将其转换为Python?正则表达式用于匹配ipv4地址,但是有更好的方法来匹配它吗?
if ($line =~ m{\s+id\s+(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}),\s+data\s+(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}),\s+Type Transit\s+(\d{1,2})}) {
$id = "$1.$2.$3.$4";
$data = "$5.$6.$7.$8";
}
match = re.search(r"\s+id\s+(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}),\s+data\s+(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}),\s+Type Transit\s+(\d{1,2})", subject)
if match:
id = ".".join(match.group(1,2,3,4))
data = ".".join(match.group(5,6,7,8))
else:
# Match attempt failed
Run Code Online (Sandbox Code Playgroud)