string strList = "SIGNAL0.95OFFSET124.60DRIFT-1.09REC.BACKSCATTER877CHANGE2TR.BACKSCATTER9.9CHANGE0.1TE27.6VBB16.6VH0.5LEDI5.7P1514.8M15-14.9BGND-0.2AMBL-0.7DUTY1.6DRI21MEASUREMENTSTS28.3DRDINST768DRY768.8HARDWARE:OK";
如上字符串 加入我想要取SIGNAL的值,只需指定SIGNAL和OFFSET就可以获取到0.95
           取OFFSET的值,只需指定OFFSET和DRIFT就可以获取124.60
           .....
  public static string GetMiddleValue(string strList, string str1, string str2)
        {
            string str = "";
            int str1Index = strList.IndexOf(str1);
            int str2Index = strList.IndexOf(str2);
            if (str1Index > -1 && str2Index > -1 && str1Index != str2Index)
            {
                str = strList.Substring(str1Index + str1.Length, Math.Abs(str1Index - str2Index) - str2.Length-1);
            }
            return str;
        }
              
如上方法只能取第一个SIGNAL的值,取后面的就乱了,谁能帮忙看一下吗

解决方案 »

  1.   

     string strList = "SIGNAL0.95OFFSET124.60DRIFT-1.09REC.BACKSCATTER877CHANGE2TR.BACKSCATTER9.9CHANGE0.1TE27.6VBB16.6VH0.5LEDI5.7P1514.8M15-14.9BGND-0.2AMBL-0.7DUTY1.6DRI21MEASUREMENTSTS28.3DRDINST768DRY768.8HARDWARE:OK";            Regex reg = new Regex(string.Format(@"{0}([\S\s]+?){1}","SIGNAL","FFSET"));
                Match m = reg.Match (strList);
                if(m.Success)
                {
                    Response.Write(m.Result("$1"));
                }
      

  2.   


      public static string GetMiddleValue(string strList, string str1, string str2)
            {
                string str = "";
                int str1Index = strList.IndexOf(str1);
                int str2Index = strList.IndexOf(str2);
                if (str1Index > -1 && str2Index > -1 && str1Index != str2Index)
                {
                    str = strList.Substring(str1Index + str1.Length, Math.Abs(str1Index - str2Index) - str1.Length);
                }
                return str;
            }
                  
    就可以了 NND