Perl到Python Regex

Eth*_*itt 2 python regex perl

如何将其转换为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";
}

Tim*_*ker 5

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)

  • 化妆品建议:`'.'.join(match.group(1,2,3,4))`或`'.'.join(match.group(*range(1,5)))` (2认同)