字符串:P235/50R18如何把这个字符串赋值给以下三个文本框?最终结果如下:textbox1="235"
textbox2="50"
textbox3="18"字符串是变量,也可能是“P235/50 ZR18”请问如何截取这组字符串的三个数字部分?求路人解答。万分谢谢!!!

解决方案 »

  1.   

    foreach(Match m in Regex.Matches("P235/50 ZR18","\\d+"))
    {
      m.Value就是你想要的
    }
      

  2.   

    还有其他的方法吗? Regex.如何定义 vs2010里不认!
      

  3.   

    foreach(Match m in Regex.Matches("P235/50 ZR18","\\d+"))
    {
    //m.Groups[0]
    //235
    50
    18
    }
      

  4.   

    using System.Text.Regularexpression;
      

  5.   

    Regex不认。。是你没添加Regex引用
      

  6.   

    添加命名空间using System.Text.Regularexpression;foreach(Match m in Regex.Matches("P235/50 ZR18","\\d+"))
    {
    textbox1=m.Groups[0].Value;
    textbox2=m.Groups[1].Value;
    textbox3=m.Groups[2].Value;
    Response.Write(textbox1+textbox2+textbox3);
    }
      

  7.   

    谢谢楼上2位大神!添加命名空间using System.Text.Regularexpression;foreach(Match m in Regex.Matches("P235/50 ZR18","\\d+"))
    {
    textbox1=m.Groups[0].Value;
    textbox2=m.Groups[1].Value;
    textbox3=m.Groups[2].Value;
    Response.Write(textbox1+textbox2+textbox3);
    }这里只显示textbox1的值,textbox2、textbox3的值都是空;是不是得循环把Groups[0].Groups[1].Groups[2].赋值到数组里 出来再付给textbox呢?
      

  8.   

    代码片段如下:        private void c_guige_SelectedIndexChanged(object sender, EventArgs e)
            {
                t_kuandu.Text = "";
                t_bianpinglv.Text = "";
                t_gangquan .Text ="";
                foreach (Match m in Regex.Matches(c_guige.Text.Trim (), "\\d+"))
                {
                    t_kuandu.Text = m.Groups[0].Value;
                    t_bianpinglv.Text = m.Groups[1].Value;
                    t_gangquan.Text = m.Groups[2].Value;
                }        }c_guige.Text.Trim ()是comboBox控件,其内容是:145/70R12 类似这样的数字 格式比较固定,一般就是数字在变,但是t_kuandu.Text每次得到的都是最后2为数字(12),t_bianpinglv.Text、t_gangquan.Text永远为空,请教如何解决?
      

  9.   

    group是配合正则表达式里面括号用的,这里没必要用group啊,每个m.value就是你要的
      

  10.   

    杯具的孩子
    foreach (Match m in Regex.Matches(c_guige.Text.Trim (), "\\d+"))
      {
      t_kuandu.Text = m.Groups[0].Value;
      t_bianpinglv.Text = m.Groups[1].Value;
      t_gangquan.Text = m.Groups[2].Value;
      }
    亮了
      

  11.   

    谢谢楼上的朋友们给的思路,用笨方法问题解决!!!
    分享代码片段如下,供需要的朋友使用:
                    string s = c_guige.Text.Trim();
                    string pattern = "\\d+";    
                    Match result = Regex.Match(s, pattern);
                    MatchCollection results = Regex.Matches(s, pattern);
                    t_kuandu.Text = results[0].Value;
                    t_bianpinglv.Text = results[1].Value;
                    t_gangquan.Text = results[2].Value;
      

  12.   

    match捕获第一个匹配。matches会捕获所有的匹配。               string s = c_guige.Text.Trim();
                    string pattern = "\\d+";     
                    MatchCollection results = Regex.Matches(s, pattern);
                    t_kuandu.Text = results[0].Value;
                    t_bianpinglv.Text = results[1].Value;
                    t_gangquan.Text = results[2].Value;