Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2008, 03:58 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Whats the Difference Between MatchCollection and..

Hi

I am doing Regular expressions right now in C# and I don't understand which Collection should I be using?

Like I seen a couple different tutorials some Using MatchCollection and some using GroupCollection. I don't know when I should be using what.

Like Here are some ways I seen to do it.

// This is my own try at it.
Code:
  string asinPattern = "reg Expression Here;
        Regex MyRegex = new Regex(asinPattern,
      RegexOptions.IgnoreCase
      | RegexOptions.Multiline
      | RegexOptions.CultureInvariant
      | RegexOptions.IgnorePatternWhitespace
      | RegexOptions.Compiled
      );

        MatchCollection matchAsin = MyRegex.Matches(result);

        for (int i = 0; i < matchAsin.Count; i++)
            {
                // Group g = matchAsin[i];
                Match g = matchAsin[i];
                Test.Add(g.Value);
            }


So first what Is the difference between using Group or Match. They both seem to get the same result.

The second way I see it is with a for each loop.

Code:
MatchCollection matches = rx.Matches(text);
    foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }
So this one I don't understand why they first put in a match collection then a groupCollection. It seems kinda pointless to me. To keep put it in a another collection one at a time. So I got to be missing something.

http://msdn.microsoft.com/en-us/libr...ollection.aspx

Then finally I seen

Code:
using System.Text.RegularExpressions;

class Reg{
    public static void Main(){
        // Should match everything except the last two.
        string str = "$1.57 $316.15 $19.30 $0.30 $0.00 $41.10 $5.1 $.5";
        string strMatch = @"\$(\d+)\.(\d\d)";

        for ( Match m = Regex.Match( str, strMatch ); m.Success; m = m.NextMatch() ){
            GroupCollection gc = m.Groups;

            System.Console.WriteLine( "The number of captures: " + gc.Count );
            // Group 0 is the entire matched string itself
            // while Group 1 is the first group to be captured.
            for ( int i = 0; i < gc.Count; i++ ){
                Group g = gc[i];
                System.Console.WriteLine( g.Value );
            }
        }
    }
}

Where they use Group and GroupCollection. 

So when should I use each scenario? 

Thanks
 
Old May 19th, 2008, 04:24 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Generally I would say that you use a MatchCollection if you want to find any number of strings that match a particular regular expression. Each Match will be a match against the whole regular expression.

Groups can be a sub-part of a regular expression, captured inbetween parenthasis '()'.

So in the example above they want to match a dollar, followed by a number, then a period, then another number. Each instance of this sequence is a match. Within each match the first number and the second number are wrapped in parenthasis so they form two seperate groups.

Just to confuse matters a little the first group (gc[0] in the above example) will be the whole match.

/- Sam Judson : Wrox Technical Editor -/
 
Old May 19th, 2008, 05:06 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

So you might be using both Match Collection and General Collection together? Since you Might first want to get all the Matches and then break each Match down into Groups.

What does Capture Collection do then?

Thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats the work around? aaronmuslim XSLT 5 March 26th, 2008 04:10 PM
Whats this book like? PaulBerry BOOK: Beginning C# 2005 Databases 0 January 25th, 2008 12:55 PM
Whats wrong? Agentofnight Beginning PHP 3 April 17th, 2005 04:11 AM
Whats the diff?? nsakic XSLT 2 January 17th, 2004 07:11 PM
Try this and tell me whats the problem? nsakic MySQL 1 January 8th, 2004 06:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.