tags:

views:

58

answers:

2

For example, I have a pattern that I am searching for using the \G option so it remembers its last search. I would like to be able to reuse these in .NET c# (ie: save the matches into a collection)

For Example:

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

I hope this is clear, I can elaborate if not.

Thanks :-)

+1  A: 

try this:

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;
        }
Jay
A: 

This simple?

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