2016-09-21 173 views
-1

我試圖做一些工作,我發現在MSDN上,並尋求一些幫助。我試圖找到哪裏有(星號,通配符,星號)將在字符串的末尾,並返回該匹配。我的模式在哪裏出錯?C#正則表達式字符串結尾

static void Main(string[] args) 
    { 
     string pattern; 
     pattern = ("*$"); 
     Regex rgx = new Regex(pattern); 

     string[] tests = 
     { 
      "42", ".45", "3452013232", "2015550777*" 
     }; 

     foreach (string test in tests) 
     { 
      if(rgx.IsMatch(test)) 
      Console.WriteLine(test); 
      else 
      Console.WriteLine("No Matches!"); 
     } 
     Console.ReadLine(); 
    } 

謝謝!

+0

你爲什麼不只是使用'string.EndsWith()' – Jonesopolis

+0

你爲什麼不使用'。載有()方法,或IndexOf'確定字符串的結尾.. 。?或'EndsWIth()' – MethodMan

+0

我必須使用正則表達式,因爲它是我不能控制的更大項目的一部分,我的老闆是。 – MrASifuMason

回答

3

Asterisk在正則表達式中有特殊含義。如果你想匹配一個實際的星號,你必須逃避它。嘗試:

pattern = @"\*$" 
+0

謝謝,它返回了測試數組中的所有字符串。 – MrASifuMason

相關問題