如何从有或没有foreach循环的Match中获取每个值

时间:2017-01-09 16:26:53

标签: loops foreach match

我试图将来自match()方法的4个值一个接一个地插入到4个不同的文本框中,但是在第一个值放入第一个文本框后,无法设法从循环中退出,这使得4值逐个出现在第一个文本框中,然后跳转到下一个文本框以执行相同操作。这是我的代码:

       for (int g = 0; g <5; g++)  //tried this inside foreach() ; no result
                         {
                                 foreach (Match m in mc)
                                 {
                                     /////each foreach returns 4 value, which then should be feed to textbox13 to 16, one by one.
                                     if (g == 1) { textBox13.Text = m.Groups[0].Value; MessageBox.Show(m.Groups[0].Value); continue; }
                                     //but continue;'s are not breaking the loop
                                         if (g == 2) { textBox14.Text = m.Groups[0].Value; MessageBox.Show(m.Groups[0].Value); continue; }
                                       //all four value appears in the first textbox(textBox13), one by one, then jumps to 2nd textbox(textBox14) and does the same w/ the same values
                                     ////until the 4th textbox (textbox16)...

                                             if (g == 3) { textBox15.Text = m.Groups[0].Value; MessageBox.Show(m.Groups[0].Value); continue; }

                                                 if (g == 4)
                                                 {
                                                     textBox16.Text = m.Groups[0].Value; MessageBox.Show(m.Groups[0].Value);
                                                     sendrow();
                                                     textBox13.Text = "";
                                                     textBox14.Text = "";
                                                     textBox15.Text = "";
                                                     textBox16.Text = "";
                                                     g = 0;          // then all textboxes emptied for the next group of 4 values...
                                                     continue;       //coming from a higher-loop, before the 1st for(), which is NOT shown here.
                                     }
                         }
                     }

我在哪里犯了foreach()循环错误?这个匹配的功能是否可以通过索引选择任何值,就像我们使用它的.Groups [index] .Value?< / p>

谢谢。

1 个答案:

答案 0 :(得分:0)

这完成了这项工作。很抱歉打扰。(如果与其他人无关,请删除)

                     ICollection<string> mc = Regex.Matches(ztring, @"\d+(\.\d{1,4})?").Cast<Match>()
                     .Select(x => x.Groups[0].Value)
                     .ToList();

                     foreach (string m in mc)
                     { MessageBox.Show(m);
                         textBox13.Text += Environment.NewLine + m; }

                                 for (int f = 0; f < textBox13.Lines.Length+1; f++)
                                 {
                                     string opn_val= textBox10.Lines[0];
                                     string high_val = textBox10.Lines[1];
                                     string low_val = textBox10.Lines[2];
                                     string close_val = textBox10.Lines[3];

                                     if (f == 1) { MessageBox.Show(textBox10.Lines[0]); }
                                     if (f == 2) { MessageBox.Show(textBox10.Lines[1]); }
                                     if (f == 3) { MessageBox.Show(textBox10.Lines[2]); }
                                     if (f == 4) { MessageBox.Show(textBox10.Lines[3]); }
                                 }
                    }