2013-12-13 87 views
-2

我的REGEX工程here但不在代碼中。我從來沒有在C#中完成過REGEX,所以我可能會在語法上丟失一些東西。有任何想法嗎?C#正則表達式語法

基本上我想通讀一個文件old.txt,如果一行符合我的REGEX,那麼我想替換它。我想要做的是讀每一行,用我的REGEX檢查它(如果它不匹配,則罰款;如果是,則更改)然後將該行寫入另一個文件new.txt

截至目前我正在寫控制檯進行測試

static void Main(string[] args) 
    { 
     String line; 

     try 
     { 
      //Pass the file path and file name to the StreamReader constructor 
      StreamReader sr = new StreamReader("C:\\old.txt"); 

      //Read the first line of text 
      line = sr.ReadLine(); 

      //Continue to read until you reach end of file 
      while (line != null) 
      { 
       //check lines 
       if (Regex.IsMatch(line, @"\s?(set)\s*(\w+):?(\d)\s+;?(.*)?")) 
       { 
        line = Regex.Replace(line, @"\s?(set)\s*(\w+):?(\d)\s+;?(.*)?", "$1 $2:$3  :Integer // $4"); 
       } 

       if (Regex.IsMatch(line, @"\s?(string)\s*(\w+)\((\d)\)\s*;(.*)")) 
       { 
        line = Regex.Replace(line, @"\s?(string)\s*(\w+)\((\d)\)\s*;(.*)", "$1 $2($3) :array [0..$3] of AnsiChar; // $4"); 
       } 
       //write the lie to console window 
       Console.WriteLine(line); 
       //Read the next line 
       line = sr.ReadLine(); 
      } 

      //close the file 
      sr.Close(); 
      Console.ReadLine(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Exception: " + e.Message); 
     } 
     finally 
     { 
      Console.WriteLine("Executing finally block."); 
     } 
    } 
+3

什麼是其中一些讓你意想不到的輸出輸入,什麼是輸出,和你有什麼期望? – Sean

+0

是否有例外? –

+0

@Sean你可以在我提供的鏈接中看到它。不,沒有例外 –

回答