0

我正在尝试查找文件中的所有 IP 地址并将其替换为此模式“ * ”,但我最终匹配了看起来像 IP 地址的虚线字符串并替换/修改它们。这里有出路吗?也许使用 ip 包中的 tcl 内置函数?

代码看起来像这样:

proc hide_ip { in_file } \
{
    set input_file [open $in_file]
    set file_content [read $input_file]
    set changed false 


    set ip_tags [list {(((25[0-5])|(2[0-4]\d)|(1\d\d)|(0?\d?\d))\.((25[0-5])|(2[0-4]\d)|(1\d\d)|(0?\d?\d))\.((25[0-5])|(2[0-4]\d)|(1\d\d)|(0?\d?\d))\.((25[0-5])|(2[0-4]\d)|(1\d\d)|(0?\d?\d))(/((3[0-2])|([1-2]?\d)))?)} \
                      {(((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?(/((12[0-8])|(1[0-1]\d)|(0?\d?\d)))?)}]

foreach item $ip_tags {
        set substituted [regsub -all -line -- $item $file_content {***} file_content]
        set changed [expr {$changed || $substituted}]
    }

...

192.168.1.1 --> 这应该匹配

12.192.168.1.1.567 --> 这不应该匹配

4

2 回答 2

1

您可以使用\b标记正则表达式的边界,使其与内部的边界不匹配12.192.168.1.1.567

于 2013-11-08T12:01:41.417 回答
0

如果你想匹配192.168.1.1但不匹配12.192.168.1.1.567,你可以使用否定的前瞻来排除第二个匹配。

于 2013-11-08T10:49:06.227 回答