2009-07-23 5 views
0

たとえば、私は\Gオプションを使用して検索しているパターンを持っていますので、最後の検索を覚えています。例えば.NETで使用するRegEx.Replaceで使用された変数を取得する方法はありますか?

を:私は(コレクションに試合を保存IE):.NETのC#でこれらを再利用できるようにしたいと思いますされていない場合

string pattern = @"\G<test:Some\s.*"; 
string id = RegEx.Match(orig, pattern).Value; 
// The guy above has 3 matches and i want to save all three into a generic list 

私は、これは明らかであると思い、私は手の込んだことができます。

private void btnEval_Click(object sender, EventArgs e) 
     { 
      txtOutput.Text = ""; 
      try 
      { 
       if (Regex.IsMatch(txtInput.Text, txtExpression.Text, getRegexOptions())) 
       { 
        MatchCollection matches = Regex.Matches(txtInput.Text, txtExpression.Text, getRegexOptions()); 

        foreach (Match match in matches) 
        { 
         txtOutput.Text += match.Value + "\r\n"; 
        } 

        int i = 0; 
       } 
       else 
       { 
        txtOutput.Text = "The regex cannot be matched"; 
       } 
      } 
      catch (Exception ex) 
      { 
       // Most likely cause is a syntax error in the regular expression 
       txtOutput.Text = "Regex.IsMatch() threw an exception:\r\n" + ex.Message; 
      } 

     } 

     private RegexOptions getRegexOptions() 
     { 
      RegexOptions options = new RegexOptions(); 

      return options; 
     } 
+0

完全な例を挙げると役立ちます。 –

答えて

1

:-)

おかげでこれを試して?

List<string> matches = new List<string>(); 
matches.AddRange(Regex.Matches(input, pattern)); 
0

このシンプル:

関連する問題