2012-12-23 87 views
1
using System; 
using System.IO; 

class Test 
{ 
    public static void Main() 
    { 
     try 
     { 
      using (StreamReader sr = new StreamReader("TestFile.txt")) 
      { 
       String line = sr.ReadToEnd(); 
       Console.WriteLine(line); 
      } 

      string pattern1 = @"(in cm)"; 
      string pattern2 = @"mm"; 
      Regex rgx1 = new Regex(pattern1); 
      Regex rgx2 = new Regex(pattern2); 
      Regex rgx3 = new Regex(pattern3); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("The file could not be read:"); 
      Console.WriteLine(e.Message); 
     } 
    } 
} 

如何使用正則表達式在C#以下釐米的文件轉換到M:正則表達式轉換輸入C#

rectangle (in cm) 20 H * 40 W and circle diameter 30 mm 

回答

0

你的主要內使用這樣的()..

string s1="", s2; 
    try 
    { 
     using (StreamReader sr = new StreamReader("C:\\test.txt")) //in my case 
     { 
      s1 = sr.ReadToEnd(); 
     } 

    } 
    catch (Exception ei) { MessageBox.Show(ei.Message); } 

    Console.WriteLine(s1); 

    Regex r = new Regex(@"\bcm\b"); //regex for changing pattern 
    s2 = r.Replace(s1, "m"); 

    Console.WriteLine(s2); 
相關問題