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