2016-11-13 31 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); 
相關問題