Csharp/CSharp Tutorial/Regular Expression/Regex Group

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Get Group in a match

using System;
using System.Text.RegularExpressions;
public class EntryPoint
{
    static void Main( string[] args ) {
        // Create regex to search for IP address pattern.
        string pattern = @"([01]?\d\d?|2[0-4]\d|25[0-5])\." +
                         @"([01]?\d\d?|2[0-4]\d|25[0-5])\." +
                         @"([01]?\d\d?|2[0-4]\d|25[0-5])\." +
                         @"([01]?\d\d?|2[0-4]\d|25[0-5])";
        Regex regex = new Regex( pattern );
        Match match = regex.Match( "192.168.169.1" );
        while( match.Success ) {
            Console.WriteLine( "IP Address found at {0} with " +
                               "value of {1}",
                               match.Index,
                               match.Value );
            Console.WriteLine( "Groups are:" );
            foreach( Group g in match.Groups ) {
                Console.WriteLine( "\t{0} at {1}",
                                   g.Value,
                                   g.Index );
            }
            match = match.NextMatch();
        }
        
    }
}
IP Address found at 0 with value of 192.168.169.1
Groups are:
        192.168.169.1 at 0
        192 at 0
        168 at 4
        169 at 8
        1 at 12

Match Group Value

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
public class MainClass{
   public static void Main(){
        Regex r = new Regex(@"<([^>]+)>([^<]*)</(\1)>");
        Match m = r.Match("<M>S</M>");
        Console.WriteLine("Tag: {0}, InnerText: {1}", m.Groups[1].Value, m.Groups[2].Value);
   }
}
Tag: M, InnerText: S

Name a Regex group

using System;
using System.Text.RegularExpressions;
class MainClass
{
  public static void Main()
  {
    string text = "(800) 888-1211\n" +
      "(212) 555-1212\n" +
      "(506) 777-1213\n" +
      "(650) 222-1214\n" +
      "(888) 111-1215\n";
    string areaCodeRegExp = @"(?<areaCodeGroup>\(\d\d\d\))";
    string phoneRegExp = @"(?<phoneGroup>\d\d\d\-\d\d\d\d)";
    MatchCollection myMatchCollection = Regex.Matches(text, areaCodeRegExp + " " + phoneRegExp);
    foreach (Match myMatch in myMatchCollection)
    {
      Console.WriteLine("Area code = " + myMatch.Groups["areaCodeGroup"]);
      Console.WriteLine("Phone = " + myMatch.Groups["phoneGroup"]);
      foreach (Group myGroup in myMatch.Groups)
        foreach (Capture myCapture in myGroup.Captures)
          Console.WriteLine("myCapture.Value = " + myCapture.Value);
    }
  }
}
Area code = (800)
Phone = 888-1211
myCapture.Value = (800) 888-1211
myCapture.Value = (800)
myCapture.Value = 888-1211
Area code = (212)
Phone = 555-1212
myCapture.Value = (212) 555-1212
myCapture.Value = (212)
myCapture.Value = 555-1212
Area code = (506)
Phone = 777-1213
myCapture.Value = (506) 777-1213
myCapture.Value = (506)
myCapture.Value = 777-1213
Area code = (650)
Phone = 222-1214
myCapture.Value = (650) 222-1214
myCapture.Value = (650)
myCapture.Value = 222-1214
Area code = (888)
Phone = 111-1215
myCapture.Value = (888) 111-1215
myCapture.Value = (888)
myCapture.Value = 111-1215

RegEx Group

using System;
using System.Text.RegularExpressions;
    class Test
    {
        public static void Main()
        {
            string string1 = "04:03:27 127.0.0.0 yourdomain.ru";
            Regex theReg = new Regex(@"(?<time>(\d|\:)+)\s" +
                @"(?<ip>(\d|\.)+)\s" +
                @"(?<site>\S+)");
            MatchCollection theMatches = theReg.Matches(string1);
            foreach (Match theMatch in theMatches)
            {
                if (theMatch.Length != 0)
                {
                    Console.WriteLine("\ntheMatch: {0}",theMatch.ToString());
                    Console.WriteLine("time: {0}",theMatch.Groups["time"]);
                    Console.WriteLine("ip: {0}",theMatch.Groups["ip"]);
                    Console.WriteLine("site: {0}",theMatch.Groups["site"]);
                }
            }
        }
    }