2009-07-23 46 views
0

例如,我有一个模式,我正在使用\G选项搜索,因此它会记住它的上一次搜索。我希望能在.NET C#重用这些(即:保存匹配到一个集合)有没有办法让在RegEx.Replace中使用的变量在.NET中使用?

例如:

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 

我希望这是明确的,我如果不细说。

感谢:-)

+0

如果你给一个完整的例子,这将有所帮助。 – 2009-07-23 17:53:49

回答

1

试试这个:

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

这个简单的?

List<string> matches = new List<string>(); 
matches.AddRange(Regex.Matches(input, pattern)); 
相关问题