The Labs \ Source Viewer \ SSCLI \ System.Text.RegularExpressions \ Capture

  1. //------------------------------------------------------------------------------
  2. // <copyright file="RegexCapture.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. // Capture is just a location/length pair that indicates the
  16. // location of a regular expression match. A single regexp
  17. // search may return multiple Capture within each capturing
  18. // RegexGroup.
  19. namespace System.Text.RegularExpressions
  20. {
  21.    
  22.     /// <devdoc>
  23.     /// <para>
  24.     /// Represents the results from a single subexpression capture. The object represents
  25.     /// one substring for a single successful capture.</para>
  26.     /// </devdoc>
  27.     [Serializable()]
  28.     public class Capture
  29.     {
  30.         internal string _text;
  31.         internal int _index;
  32.         internal int _length;
  33.        
  34.         internal Capture(string text, int i, int l)
  35.         {
  36.             _text = text;
  37.             _index = i;
  38.             _length = l;
  39.         }
  40.        
  41. /*
  42.         * The index of the beginning of the matched capture
  43.         */       
  44.         /// <devdoc>
  45.         /// <para>Returns the position in the original string where the first character of
  46.         /// captured substring was found.</para>
  47.         /// </devdoc>
  48.         public int Index {
  49.             get { return _index; }
  50.         }
  51.        
  52. /*
  53.         * The length of the matched capture
  54.         */       
  55.         /// <devdoc>
  56.         /// <para>
  57.         /// Returns the length of the captured substring.
  58.         /// </para>
  59.         /// </devdoc>
  60.         public int Length {
  61.             get { return _length; }
  62.         }
  63.        
  64.         /// <devdoc>
  65.         /// <para>[To be supplied.]</para>
  66.         /// </devdoc>
  67.         public string Value {
  68.             get { return _text.Substring(_index, _length); }
  69.         }
  70.        
  71. /*
  72.         * The capture as a string
  73.         */       
  74.         /// <devdoc>
  75.         /// <para>
  76.         /// Returns
  77.         /// the substring that was matched.
  78.         /// </para>
  79.         /// </devdoc>
  80.         public override string ToString()
  81.         {
  82.             return Value;
  83.         }
  84.        
  85. /*
  86.         * The original string
  87.         */       
  88.         internal string GetOriginalString()
  89.         {
  90.             return _text;
  91.         }
  92.        
  93. /*
  94.         * The substring to the left of the capture
  95.         */       
  96.         internal string GetLeftSubstring()
  97.         {
  98.             return _text.Substring(0, _index);
  99.         }
  100.        
  101. /*
  102.         * The substring to the right of the capture
  103.         */       
  104.         internal string GetRightSubstring()
  105.         {
  106.             return _text.Substring(_index + _length, _text.Length - _index - _length);
  107.         }
  108.        
  109.         #if DBG
  110.         internal virtual string Description()
  111.         {
  112.             StringBuilder Sb = new StringBuilder();
  113.            
  114.             Sb.Append("(I = ");
  115.             Sb.Append(_index);
  116.             Sb.Append(", L = ");
  117.             Sb.Append(_length);
  118.             Sb.Append("): ");
  119.             Sb.Append(_text, _index, _length);
  120.            
  121.             return Sb.ToString();
  122.         }
  123.         #endif
  124.     }
  125.    
  126.    
  127.    
  128. }

Developer Fusion