-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegex.cs
More file actions
296 lines (248 loc) · 8.45 KB
/
Regex.cs
File metadata and controls
296 lines (248 loc) · 8.45 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Collections.Generic;
using System.Linq;
using ExRegex.Match;
using ExRegex.Regexies;
namespace ExRegex
{
/// <summary>
/// 正規表現
/// </summary>
public abstract class Regex
{
public abstract string Name { get; }
/// <summary>
/// 親子関係は考慮しない単純なクローン
/// </summary>
/// <returns></returns>
protected abstract Regex SingleClone();
/// <summary>
/// 後続を接続
/// </summary>
/// <param name="nextRegex"></param>
public virtual Regex To(Regex nextRegex)
{
var clone = SingleClone();
if (nextRegex is Empty)
{
return clone;
}
if (_nextRegex != null)
{
clone._nextRegex = _nextRegex.To(nextRegex);
}
else
{
clone._nextRegex = nextRegex;
}
return clone;
}
/// <summary>
/// 範囲重複を許容せず最高優先マッチ箇所をすべて列挙
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public IEnumerable<RegexMatch> MatchesRegular(StringPointer str)
{
for (int i = 0; i < str.Length + 1; i++)
{
var subStr = str.SubString(i);
var match = HeadMatch(subStr, new MatingContext());
if (match != null)
{
yield return match;
i += Math.Max(match.Length - 1, 0);
}
}
}
/// <summary>
/// マッチ箇所を全て列挙。重複箇所も。
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public IEnumerable<RegexMatch> Matches(string str)
{
return Matches((StringPointer)str, new MatingContext());
}
/// <summary>
/// 完全マッチ
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public IEnumerable<RegexMatch> PerfectMatches(string str)
{
return PerfectMatches((StringPointer)str,new MatingContext());
}
/// <summary>
/// 先頭マッチ
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public IEnumerable<RegexMatch> HeadMatches(string str)
{
return HeadMatches((StringPointer)str,new MatingContext());
}
/// <summary>
/// 末尾マッチ
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public IEnumerable<RegexMatch> TailMatches(string str)
{
return TailMatches((StringPointer)str,new MatingContext());
}
/// <summary>
/// 最初の完全マッチ
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public RegexMatch PerfectMatch(string str)
{
return PerfectMatches(str).FirstOrDefault();
}
/// <summary>
/// 最初の先頭マッチ
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public RegexMatch HeadMatch(string str)
{
return HeadMatches(str).FirstOrDefault();
}
/// <summary>
/// 最初の末尾マッチ
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public RegexMatch TailMatch(string str)
{
return TailMatches(str).FirstOrDefault();
}
/// <summary>
/// SingleCloneして親子関係も複製
/// </summary>
/// <returns></returns>
public Regex Clone()
{
var clone = SingleClone();
if (_nextRegex != null)
{
clone._nextRegex = _nextRegex.Clone();
}
return clone;
}
protected IEnumerable<RegexMatch> PerfectMatches(StringPointer str, MatingContext context)
{
return HeadMatches(str, context).Where(m => m.Length == str.Length);
}
private Regex _nextRegex;
/// <summary>
/// このRegexChainの再優先マッチを取得。マッチしなければnull
/// </summary>
/// <param name="str"></param>
/// <param name="context"></param>
/// <returns></returns>
public RegexMatch HeadMatch(StringPointer str, MatingContext context)
{
return HeadMatches(str,context).FirstOrDefault();
}
public bool IsPerfectMatch(string str)
{
return PerfectMatch(str) != null;
}
public bool IsHeadMatch(string str)
{
return HeadMatch((StringPointer) str, new MatingContext()) != null;
}
/// <summary>
/// 文字列の先頭から、後続を考慮したマッチを優先度順に列挙
/// </summary>
/// <param name="str"></param>
/// <param name="context"></param>
/// <returns></returns>
public IEnumerable<RegexMatch> HeadMatches(StringPointer str,MatingContext context)
{
if (_nextRegex == null)//末端は自身のマッチを返せばいい。
{
foreach (var matching in SimpleMatchings(str, context))
{
yield return matching;
}
}
else//末端以外は、自分以降にマッチが存在するマッチを返す。
{
foreach (var selfMatch in SimpleMatchings(str, context))
{
var nextStr = str.SubString(selfMatch.Length);
foreach (var nextMatch in _nextRegex.HeadMatches(nextStr, context))
{
yield return new ArrayMatch(str, selfMatch, nextMatch);
}
}
}
}
/// <summary>
/// 文字列の末尾で終わる、後続を考慮したマッチを優先度順に列挙
/// (後よみ用?)
/// </summary>
/// <param name="str"></param>
/// <param name="context"></param>
/// <returns></returns>
public IEnumerable<RegexMatch> TailMatches(StringPointer str, MatingContext context)
{
return Matches(str,context).Where(match => match.AfterStr == "");
}
/// <summary>
/// テキスト全体でマッチ箇所を全て列挙
/// </summary>
/// <param name="str"></param>
/// <param name="context"></param>
/// <returns></returns>
public IEnumerable<RegexMatch> Matches(StringPointer str, MatingContext context)
{
for (int i = 0; i < str.Length + 1; i++)
{
var subStr = str.SubString(i);
foreach (var match in HeadMatches(subStr,new MatingContext()))
{
yield return match;
}
}
}
/// <summary>
/// 与えられた文字列が先頭からこの正規表現にマッチするか判定。後続のことは考慮しない。
/// </summary>
/// <param name="str"></param>
/// <param name="context"></param>
/// <returns>マッチ優先度高い順のマッチ。マッチしないときは空のIEnumerable</returns>
public abstract IEnumerable<RegexMatch> SimpleMatchings(StringPointer str, MatingContext context);
/// <summary>
/// 空のRegex作るだけ
/// </summary>
/// <returns></returns>
public static Regex Make()
{
return new Empty();
}
/// <summary>
/// C#はキャストをオーバーロードできる!!すごい!!
/// </summary>
/// <param name="str"></param>
public static implicit operator Regex(string str)
{
return new Literal(str);
}
public override string ToString()
{
return Name;
}
protected virtual string StructureString()
{
return ToString();
}
public string ToStructureString()
{
return String.Format("{0}{1}",StructureString(),_nextRegex!=null?"\n"+_nextRegex.ToStructureString():"");
}
}
}