一段字符串如下:Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.E:\工作\PingServer\PingServer\bin\Debug>ping -n 1 [192.168.0.145]Pinging A.redirect.local [221.231.141.211] with 32 bytes of data:Ping statistics for 221.231.141.211:    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
E:\工作\PingServer\PingServer\bin\Debug>exit我要截取上面的[221.231.141.211],
因为ping -n 1后面有时是[192.168.0.145],有时是navistar.uicp.net,有时又是别的,所以我不能用.Split('[')[3].Split(']')[0]来截取,
必须从后往前截取,取第一个"["开始的,"]"结束的字符串!请大家帮忙想一下!

解决方案 »

  1.   

     string arr="sfs231.31.123.123dsf";
            Regex rgx = new Regex(@"\[(\d){1,3}.(\d){1,3}.(\d){1,3}.(\d){1,3}\]");
            MatchCollection math=rgx.Matches(arr);
            foreach (Match match in math)
                Console.WriteLine(match.Value);
      

  2.   

    [color=#FF0000][/string arr="sfs[231.31.123.123]dsf"; 
            Regex rgx = new Regex(@"\[(\d){1,3}.(\d){1,3}.(\d){1,3}.(\d){1,3}\]"); 
    color]要匹配[]就这种。如果不匹配[]就把\[和\]去掉
      

  3.   

    string str ="..";
    int index = str.IndexOf("Ping statistics for ")+20;
    string ip = str.Substring(index,str.IndexOf(":",index)-index);
      

  4.   


    Regex re = new Regex("(?<=pinging[^[]*)\[(\d){1,3}.(\d){1,3}.(\d){1,3}.(\d){1,3}\]");
    MatchCollection mc = re.Matches(input);
    foreach(Match m in mc)
    {
        Console.WriteLine(m.Value);
    }
      

  5.   

    楼上正确了哈,不过有一个遗漏,那个.必须得转义掉哈(?<!ping[^[]*)\[\d+\.\d+\.\d+\.\d+\]
      

  6.   

    System.Text.RegularExpressions.Regex.Match(strIN, @"[\d.]+(?=[:])").Value;
      

  7.   


    Regex re = new Regex("(?<=pinging[^[]*)\[(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}\]");
    MatchCollection mc = re.Matches(input);
    foreach(Match m in mc)
    {
        Console.WriteLine(m.Value);
    }
    //这回对了吧  哈哈
      

  8.   

    楼上几位说的,用正则表达式从pinging开始找匹配的,但是我的这段字符串有时不一定有pinging!
      

  9.   

    Regex rgx = new Regex(@"\[(\d){1,3}.(\d){1,3}.(\d){1,3}.(\d){1,3}\]"); 这个就是匹配的[111.111.11.11]IP地址类型的
      

  10.   

    用split以"["分成两个字符串,再截取后面一个字符串中"]"字符前的内容.
      

  11.   

    7楼的是截取":"前面的,那段IP,但是这样不行,一定要是"["开始,"]"结束的,因为这两个字符之间的和":"前面的IP,可能会不一样,不管怎么样,还是非常感谢你的热心帮助!
      

  12.   

    string[] sArray_A=str.Split('['); 
    string[] sArray_B=sArray_A[sArray_A.Length-1].Split('['); 
    string   IPstr=sArray_B[0];C#没有用过,也不知道上面的代码是否有问题。
      

  13.   

    string[] sArray_A=str.Split('['); 
    string[] sArray_B=sArray_A[sArray_A.Length-1].Split(']'); 
    string  IPstr=sArray_B[0]; C#没有用过,也不知道上面的代码是否有问题。
      

  14.   

    不喜欢正则表达式想到是递归就心寒String TestStr = "ajfs[123]jkfljwejaf";
                int Flag1;
                int Flag2;
                Flag2 = TestStr.LastIndexOf(']');
                if (Flag2 > 0)
                {
                    Flag1 = TestStr.LastIndexOf('[', Flag2);
                    if (Flag1 > -1)
                    {
                        Flag1 = Flag1 + 1;
                        String result = TestStr.Substring(Flag1, Flag2 - Flag1);
                        MessageBox.Show(result);
                    }                
                }
      

  15.   

    支持用正则来解决
     private static void IPstring()
            {
                string input = @"kasdfj;jkldaf[111.111.111.111],sadf
                                   sdf[222.222.222.222]";            string pattern = @"\[(\d{3}\.){3}\d{3}\]";            Regex r = new Regex(pattern);
                
                MatchCollection m = r.Matches(input);            List<string> listStr = new List<string>();
                foreach (Match nextMatch in m)
                {
                    listStr.Add(nextMatch.ToString().Trim());
                    GroupCollection g = nextMatch.Groups;
                }  
            }
    listStr 中结果为[111.111.111.111],[222.222.222.222]
      

  16.   

    更正一下, string pattern = @"\[(\d{1,3}\.){3}\d{1,3}\]";
     如不想要'[',']',可以用string pattern = @"(\d{1,3}\.){3}\d{1,3}";
    private static void IPstring()
            {
                string input = @"kasdfj;jkldaf[111.111.111.111],sadf
                                   sdf[222.222.222.222] dsfds    d
                                  [1.2.2.33]";            string pattern = @"\[(\d{1,3}\.){3}\d{1,3}\]";            Regex r = new Regex(pattern);
                
                MatchCollection m = r.Matches(input);            List<string> listStr = new List<string>();
                foreach (Match nextMatch in m)
                {
                    listStr.Add(nextMatch.ToString().Trim());
                    GroupCollection g = nextMatch.Groups;
                }  
            }
    listStr 中结果为[111.111.111.111],[222.222.222.222],[1.2.2.33]
    这样可以满足你所说的要求,但有一个缺陷是[999.999.999.999]也能满足