2016-11-13 17 views
0

我需要在c#中编写一个Windows窗体,它需要一个文本框和一个按钮。在文本框中我必须键入例如编程指令: 为(I = 0;我< 10; i ++在)在文本框中查找单词并在数据网格中显示

然后点击一个按钮,并在数据网格它应显示这样的事:

  1. 为 - 周期
  2. ( - agrupation
  3. I - 变量
  4. = - asignation

如何识别文本的各个部分?

我试过的foreach焦炭但我真的搞砸了:(帮助请

+2

欢迎到SO。请发布您的代码。 –

回答

0

这里是一个解决方案,你可以使用我拼凑起来的,我强烈建议你熟悉你的使用正则表达式:

https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

,这里是一个很好的测试,我用: http://regexstorm.net/tester

using System.Text.RegularExpressions; 

string input = "for(i=0;i<10;i++)"; 
     string pattern = @"^(\w+)(\W)(\w)(\W).*$"; 
     MatchCollection matches = Regex.Matches(input, pattern); 

     string cycle = matches[0].Groups[1].Value; 
     string agrupation = matches[0].Groups[2].Value; 
     string variable = matches[0].Groups[3].Value; 
     string asignation = matches[0].Groups[4].Value; 

     string test = string.Format("cycle: {0}, agrupation: {1}, variable={2}, asignation: {3}", cycle, agrupation, variable, asignation); 

     Console.WriteLine(test); 
相关问题