将变量粘贴到正则表达式中

时间:2014-05-07 09:53:33

标签: c# regex

帮助将变量zone粘贴到正则表达式模式中。在我看来,我已经尝试了所有可能的变种......总是出现错误!! (这里改为静态字符串UntrustIng)

            List<string> zones = new List<string>
            { "UntrustGi",
                "DMZPub",
                "DMZMMS", 
                "DmzGi", 
                "TrustGi", 
                "TrustGn", 
                "DmzGn", 
                "UntrustGn",
                "DMZLOM" };


            foreach (string zone in zones)
            {
                string patternZone = @"set address ""(UntrustGn)"" ("".*"") ([0-9]+.[0-9]+.[0-9]+.[0-9]+)( [0-9]+.[0-9]+.[0-9]+.[0-9]+)";

2 个答案:

答案 0 :(得分:1)

使用string.Format,如下所示:

foreach (string zone in zones) {
    string patternZone = string.Format(@"set address ""{0}"" ("".*"") ([0-9]+.[0-9]+.[0-9]+.[0-9]+)( [0-9]+.[0-9]+.[0-9]+.[0-9]+)", zone);
    ...
}

答案 1 :(得分:1)

这样做:

foreach(string zone in zones)
{
   string ptternZone = string.Format(@"set address ""{0}"" ("".*"") ([0-9]+.[0-9]+.[0-9]+.[0-9]+)( [0-9]+.[0-9]+.[0-9]+.[0-9]+)", zone);
}

以上使用string.Format方法。