1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
//
// assembly: System
// namespace: System.Text.RegularExpressions
// file: replace.cs
//
// author: Dan Lewis (dlewis@gmx.co.uk)
// (c) 2002
using System;
using System.Text;
using System.Collections;
using Parser = System.Text.RegularExpressions.Syntax.Parser;
namespace System.Text.RegularExpressions {
class ReplacementEvaluator {
public static string Evaluate (string replacement, Match match) {
ReplacementEvaluator ev = new ReplacementEvaluator (match.Regex, replacement);
return ev.Evaluate (match);
}
public ReplacementEvaluator (Regex regex, string replacement) {
this.regex = regex;
terms = new ArrayList ();
Compile (replacement);
}
public string Evaluate (Match match) {
StringBuilder result = new StringBuilder ();
foreach (Term term in terms)
result.Append (term.GetResult (match));
return result.ToString ();
}
// private
private void Compile (string replacement) {
replacement = Parser.Unescape (replacement);
string literal = "";
int ptr = 0;
char c;
Term term = null;
while (ptr < replacement.Length) {
c = replacement[ptr ++];
if (c == '$') {
if (replacement[ptr] == '$') {
++ ptr;
break;
}
term = CompileTerm (replacement, ref ptr);
}
if (term != null) {
term.Literal = literal;
terms.Add (term);
term = null;
literal = "";
}
else
literal += c;
}
if (term == null && literal.Length > 0) {
terms.Add (new Term (literal));
}
}
private Term CompileTerm (string str, ref int ptr) {
char c = str[ptr];
if (Char.IsDigit (c)) { // numbered group
int n = Parser.ParseDecimal (str, ref ptr);
if (n < 0 || n > regex.GroupCount)
throw new ArgumentException ("Bad group number.");
return new Term (TermOp.Match, n);
}
++ ptr;
switch (c) {
case '{': { // named group
string name = Parser.ParseName (str, ref ptr);
if (str[ptr ++] != '}' || name == null)
throw new ArgumentException ("Bad group name.");
int n = regex.GroupNumberFromName (name);
if (n < 0)
throw new ArgumentException ("Bad group name.");
return new Term (TermOp.Match, n);
}
case '&': // entire match
return new Term (TermOp.Match, 0);
case '`': // text before match
return new Term (TermOp.PreMatch, 0);
case '\'': // text after match
return new Term (TermOp.PostMatch, 0);
case '+': // last group
return new Term (TermOp.Match, regex.GroupCount - 1);
case '_': // entire text
return new Term (TermOp.All, 0);
default:
throw new ArgumentException ("Bad replacement pattern.");
}
}
private Regex regex;
private ArrayList terms;
private enum TermOp {
None, // no action
Match, // input within group
PreMatch, // input before group
PostMatch, // input after group
All // entire input
}
private class Term {
public Term (TermOp op, int arg) {
this.op = op;
this.arg = arg;
this.literal = "";
}
public Term (string literal) {
this.op = TermOp.None;
this.arg = 0;
this.literal = literal;
}
public string Literal {
set { literal = value; }
}
public string GetResult (Match match) {
Group group = match.Groups[arg];
switch (op) {
case TermOp.None:
return literal;
case TermOp.Match:
return literal + group.Value;
case TermOp.PreMatch:
return literal + group.Text.Substring (0, group.Index);
case TermOp.PostMatch:
return literal + group.Text.Substring (group.Index + group.Length);
case TermOp.All:
return literal + group.Text;
}
return "";
}
public TermOp op; // term type
public int arg; // group argument
public string literal; // literal to prepend
public override string ToString () {
return op.ToString () + "(" + arg + ") " + literal;
}
}
}
}
|