h_3.xml  取3
r_23.xml 取23
h_792.xml 取792
h_7374.xml 取7374以上几个文件中,我只需要那几个数字,如何截取呢,格式都一样,就是位数不一样而已,只要那个数字。

解决方案 »

  1.   

    try...            Regex reg = new Regex(@"\d+");
                MatchCollection mc = reg.Matches(yourStr);
                foreach (Match m in mc)
                {
                    richTextBox2.Text += m.Value + "\n";
                }
      

  2.   

    System.Text.RegularExpressions.Regex.Match("h_3.xml","\d+").Value;
      

  3.   


    void Main()
    {
    string s=@"h_3.xml 
    r_23.xml
    h_792.xml 
    h_7374.xml ";
       foreach(Match m in Regex.Matches(s,@"\d+"))
       {
       Console.WriteLine(m.Value);
       }
    }
    /*
    3
    23
    792
    7374
    */
      

  4.   

    string ss = "pp_213.xml";
    int start=ss.IndexOf("_")+1;
    int length = ss.IndexOf(".")-start;
    MessageBox.Show(ss.Substring(start,length));
      

  5.   

    string result = Regex.Match("h_3.html",@"\d+(?=\D+$)").Value;
      

  6.   


               string[] str = ("h_3.xml,r_23.xml,h_792.xml,h_7374.xml").Split(',');
               for (int i = 0; i < str.Length; i++) { 
                    str[i] = Regex.Match(str[i],@"\d+").Value; //正则获取数值
               }