Illustrates the use of groups and captures : Group Capture « Regular Expressions « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / C Sharp » Regular Expressions » Group CaptureScreenshots 
Illustrates the use of groups and captures
Illustrates the use of groups and captures

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  ExampleC_2.cs illustrates the use of groups and captures
*/

using System;
using System.Text.RegularExpressions;

public class ExampleC_2
{

  public static void Main()
  {

    // create a string containing area codes and phone numbers
    string text =
      "(800) 555-1211\n" +
      "(212) 555-1212\n" +
      "(506) 555-1213\n" +
      "(650) 555-1214\n" +
      "(888) 555-1215\n";

    // create a string containing a regular expression to
    // match an area code; this is a group of three numbers within
    // parentheses, e.g. (800)
    // this group is named "areaCodeGroup"
    string areaCodeRegExp = @"(?<areaCodeGroup>\(\d\d\d\))";

    // create a string containing a regular expression to
    // match a phone number; this is a group of seven numbers
    // with a hyphen after the first three numbers, e.g. 555-1212
    // this group is named "phoneGroup"
    string phoneRegExp = @"(?<phoneGroup>\d\d\d\-\d\d\d\d)";

    // create a MatchCollection object to store the matches
    MatchCollection myMatchCollection =
      Regex.Matches(text, areaCodeRegExp + " " + phoneRegExp);

    // use a foreach loop to iterate over the Match objects in
    // the MatchCollection object
    foreach (Match myMatch in myMatchCollection)
    {

      // display the "areaCodeGroup" group match directly
      Console.WriteLine("Area code = " + myMatch.Groups["areaCodeGroup"]);

      // display the "phoneGroup" group match directly
      Console.WriteLine("Phone = " + myMatch.Groups["phoneGroup"]);

      // use a foreach loop to iterate over the Group objects in
      // myMatch.Group
      foreach (Group myGroup in myMatch.Groups)
      {

        // use a foreach loop to iterate over the Capture objects in
        // myGroup.Captures
        foreach (Capture myCapture in myGroup.Captures)
        {
          Console.WriteLine("myCapture.Value = " + myCapture.Value);
        }

      }

    }

  }

}

           
       
Related examples in the same category
w___w___w_.__j__a__v__a2s__._c___o__m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.