3 // namespace: System.Text.RegularExpressions
6 // author: Dan Lewis (dlewis@gmx.co.uk)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 namespace System
.Text
.RegularExpressions
{
35 public class Capture
{
41 get { return length; }
47 return text
.Substring (index
, length
);
53 public override string ToString () {
59 internal Capture (string text
) : this (text
, 0, 0) { }
61 internal Capture (string text
, int index
, int length
) {
67 internal string Text
{
73 internal int index
, length
;
78 public class Group
: Capture
{
79 public static Group
Synchronized (Group inner
) {
80 return inner
; // is this enough?
83 public CaptureCollection Captures
{
84 get { return captures; }
88 get { return success; }
93 internal Group (string text
, int[] caps
) : base (text
) {
94 this.captures
= new CaptureCollection ();
96 if (caps
== null || caps
.Length
== 0) {
102 this.index
= caps
[0];
103 this.length
= caps
[1];
105 for (int i
= 2; i
< caps
.Length
; i
+= 2)
106 captures
.Add (new Capture (text
, caps
[i
], caps
[i
+ 1]));
110 internal Group (): base ("")
112 captures
= new CaptureCollection ();
115 private bool success
;
116 private CaptureCollection captures
;
120 public class Match
: Group
{
121 public static Match Empty
{
122 get { return empty; }
125 public static Match
Synchronized (Match inner
) {
126 return inner
; // FIXME need to sync on machine access
129 public virtual GroupCollection Groups
{
130 get { return groups; }
133 public Match
NextMatch () {
137 int scan_ptr
= regex
.RightToLeft
? Index
: Index
+ Length
;
139 // next match after an empty match: make sure scan ptr makes progress
142 scan_ptr
+= regex
.RightToLeft
? -1 : +1;
144 return machine
.Scan (regex
, Text
, scan_ptr
, text_length
);
147 public virtual string Result (string replacement
) {
148 return ReplacementEvaluator
.Evaluate (replacement
, this);
153 internal Match () : base (null, null) {
156 this.text_length
= 0;
157 this.groups
= new GroupCollection ();
162 internal Match (Regex regex
, IMachine machine
, string text
, int text_length
, int[][] grps
) :
166 this.machine
= machine
;
167 this.text_length
= text_length
;
169 this.groups
= new GroupCollection ();
171 for (int i
= 1; i
< grps
.Length
; ++ i
)
172 groups
.Add (new Group (text
, grps
[i
]));
175 internal Regex Regex
{
176 get { return regex; }
182 private IMachine machine
;
183 private int text_length
;
184 private GroupCollection groups
;
186 private static Match empty
= new Match ();