Regexp.Short表示ip地址

时间:2014-04-28 21:20:35

标签: c# regex foreach

我需要简短表示ip address.Instead 0.0.0.0 0.0.0.0应该是0.0.0.0/0例如,

源文件:

set address "UntrustGn" "172.30.2.130/32" 172.30.2.130 255.255.255.255
set address "UntrustGn" "190.8.35.230/32" 190.8.35.230 255.255.255.255
set address "UntrustGn" "217.118.69.0/24" 217.118.69.0 255.255.255.0

输出文件:

set address "UntrustGn" "172.30.2.130/32" 172.30.2.13/32
set address "UntrustGn" "190.8.35.230/32" 190.8.35.230/32
set address "UntrustGn" "217.118.69.0/24" 217.118.69.0/24
我试图做的事情。但没有达到预期的效果.... 帮助!请!

    StreamReader reader = new StreamReader(opendialog.FileName);
    string patternZone = @"set address "".*"" "".*"" [0-9]+.[0-9]+.[0-9]+.[0-9]+( [0-9]+.[0-9]+.[0-9]+.[0-9]+)";

    var matchesZone = Regex.Matches(reader.ReadToEnd(), patternZone);

    foreach(var match in matchesZone)
     {
        txt.AppendText(match.ToString().Replace(" 255.255.255.255", "/32");
     }

1 个答案:

答案 0 :(得分:0)

试试这个:

string resultString = null;
try {
    resultString = Regex.Replace(subjectString, @"""(\d+\.\d+\.\d+\.\d+)(/\d+)""\s*(\d+\.\d+\.\d+\.\d+)\s*\d+\.\d+\.\d+\.\d+", new MatchEvaluator(ComputeReplacement));
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

public String ComputeReplacement(Match m) {
    // You can vary the replacement text for each match on-the-fly
    return "\"$1$2\" $3$2";
}
相关问题