假设有一个字符串变量,储存的字符串格式是"n-CxHy" 其中n-C H  这个是固定的,x,y是数字,怎么才能通过正则表达式来提取X,Y的值呢

解决方案 »

  1.   

    try...Match m = Regex.Match(yourStr, @"(?i)n-C(?<x>\d+)H(?<y>\d+)");
    if (m.Success)
    {
        richTextBox2.Text += m.Groups["x"].Value + "\n";
        richTextBox2.Text += m.Groups["y"].Value + "\n";
    }其实这个没必在用正则的
      

  2.   

    using System.Text.RegularExpressions;
    ...
    string str = @"n-C1H1 n-C2H2";
    Match m = Regex.Match(str , @"n-C(\d+)H(\d+)");
    while  (m.Success)
    {
         string str1= m.Groups[1].Value.ToString() ;
         string str2 = m.Groups[2].Value.ToString();
         m = m.NextMatch();
    }