-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTESVT_RegexUtils.pas
More file actions
355 lines (318 loc) · 9.37 KB
/
TESVT_RegexUtils.pas
File metadata and controls
355 lines (318 loc) · 9.37 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
unit TESVT_RegexUtils;
interface
uses Windows, Messages, SysUtils, Classes, StrUtils, Controls, Forms, Graphics, math, Dialogs, StdCtrls,
TESVT_Const, TESVT_Utils, regularexpressions, regularexpressionscore, RegularExpressionsConsts, TESVT_Ressources;
type
tregExUtil = class
private
function regExExtractValueEx(RegEx: tperlRegex; const s: string): TArray<string>;
function regExReplaceValuesEx(RegEx: tperlRegex; var s: string; sReplace: TArray<string>): boolean; overload;
function regExReplaceValuesEx(RegEx: tperlRegex; var s: string; sNormalized: string): boolean; overload;
function regExInitPatternEx(r: tperlRegex; pUser, pDefault: string): string;
public
regExNumber, regExAliasStrict, regExAlias, regExWord, regExNoWhiteChar: tperlRegex;
constructor Create;
destructor Destroy; override;
function regExExtractAliasStrict(const s: string): TArray<string>;
function regExExtractAlias(const s: string): TArray<string>; overload;
function regExExtractNumber(const s: string): TArray<string>;
function regExExtractWords(const s: string): TArray<string>;
function regExExtractValues(const rEx: string; const s: string): TArray<string>; overload;
function regExExtractValues(const id: integer; const s: string): TArray<string>; overload;
function regExReplaceAlias(var s: string; sReplace: TArray<string>): boolean;
function regExReplaceAliasStrict(var s: string; sReplace: TArray<string>): boolean;
function regExReplaceNumber(var s: string; sReplace: TArray<string>): boolean;
function regExReplaceValues(const id: integer; var s: string; sReplace: TArray<string>): boolean; overload;
function regExReplaceValues(const pattern: string; var s: string; a: TArray<string>): boolean; overload;
function regExNormalizeAlias(var s: string): boolean; overload;
procedure regExNormalizeAlias(a: TArray<string>; var s: string); overload;
function regExNormalizeNumber(var s: string): boolean;
function noWhiteChar(const s: string): boolean;
procedure regExInitPattern;
end;
function ExtractArrayStrings(const input, pattern: string; var l: tstringlist): boolean;
function defineHeuristicThreshold(iWord: integer): integer;
function adjustHeuristicResult(iWord: integer; ldOut: single): single;
const
iWordThreshold: integer = 1000;
iLDWordSearchThresholdMax: integer = 10;
iLDMaxBreak: integer = 25;
var
regExUtil: tregExUtil;
implementation
function defineHeuristicThreshold(iWord: integer): integer;
begin
case iWord of
0: Result := 0;
1: Result := 1;
else
begin
Result := ceil(iWord / 3) + 1;
if Result > iLDMaxBreak then
Result := iLDMaxBreak;
end;
end;
end;
function adjustHeuristicResult(iWord: integer; ldOut: single): single;
var
tmp: integer;
begin
if ldOut = 0 then
exit(0);
tmp := floor(iWord / 15);
if ldOut <= tmp then
Result := 0.55 + (ldOut / 10.0)
else
Result := ldOut;
end;
function ExtractArrayStrings(const input, pattern: string; var l: tstringlist): boolean;
var
i: integer;
RegEx: TRegEx;
a: TArray<string>;
begin
Result := false;
RegEx := TRegEx.Create(pattern);
a := RegEx.Split(input);
if length(a) = l.count then
begin
for i := 0 to length(a) - 1 do
l[i] := a[i];
Result := true;
end;
end;
constructor tregExUtil.Create;
begin
regExNumber := tperlRegex.Create;
regExAliasStrict := tperlRegex.Create;
regExAlias := tperlRegex.Create;
regExWord := tperlRegex.Create;
regExNoWhiteChar := tperlRegex.Create;
regExAliasStrict.Options := regExNumber.Options + [preCaseLess, preUnGreedy];
regExAlias.Options := regExNumber.Options + [preCaseLess, preUnGreedy];
regExWord.Options := regExNumber.Options + [preSingleLine];
regExInitPattern;
end;
destructor tregExUtil.Destroy;
begin
regExNumber.free;
regExAliasStrict.free;
regExAlias.free;
regExWord.free;
regExNoWhiteChar.free;
end;
function tregExUtil.regExInitPatternEx(r: tperlRegex; pUser, pDefault: string): string;
begin
Result := pUser;
if Result = '' then
Result := pDefault;
try
r.RegEx := Result;
r.study;
except
Result := regExInitPatternEx(r, pDefault, pDefault);
end;
end;
procedure tregExUtil.regExInitPattern;
begin
sPref_rxPatternNumber := regExInitPatternEx(regExNumber, sPref_rxPatternNumber, rxPatternNumber);
sPref_rxPatternAlias := regExInitPatternEx(regExAlias, sPref_rxPatternAlias, rxPatternAlias);
sPref_rxPatternAliasStrict := regExInitPatternEx(regExAliasStrict, sPref_rxPatternAliasStrict, rxPatternAliasStrict);
sPref_rxPatternWord := regExInitPatternEx(regExWord, sPref_rxPatternWord, rxPatternWord);
regExInitPatternEx(regExNoWhiteChar, rxPatternNoWhite, rxPatternNoWhite);
end;
function tregExUtil.regExReplaceValues(const id: integer; var s: string; sReplace: TArray<string>): boolean;
begin
case id of
1: Result := regExReplaceNumber(s, sReplace);
2: Result := regExReplaceAlias(s, sReplace);
3: Result := regExReplaceAliasStrict(s, sReplace);
else
begin
setlength(sReplace, 0);
Result := false;
end;
end;
end;
function tregExUtil.regExReplaceAliasStrict(var s: string; sReplace: TArray<string>): boolean;
begin
Result := regExReplaceValuesEx(regExAliasStrict, s, sReplace);
end;
function tregExUtil.regExReplaceAlias(var s: string; sReplace: TArray<string>): boolean;
begin
Result := regExReplaceValuesEx(regExAlias, s, sReplace);
end;
function tregExUtil.regExReplaceNumber(var s: string; sReplace: TArray<string>): boolean;
begin
Result := regExReplaceValuesEx(regExNumber, s, sReplace);
end;
function tregExUtil.regExReplaceValuesEx(RegEx: tperlRegex; var s: string; sNormalized: string): boolean;
begin
Result := false;
try
RegEx.subject := s;
while RegEx.MatchAgain do
begin
RegEx.Replacement := sNormalized;
RegEx.Replace;
Result := true;
end;
if Result then
s := RegEx.subject;
except
end;
end;
function tregExUtil.noWhiteChar(const s: string): boolean;
begin
regExNoWhiteChar.subject := s;
Result := regExNoWhiteChar.match;
end;
function tregExUtil.regExReplaceValuesEx(RegEx: tperlRegex; var s: string; sReplace: TArray<string>): boolean;
var
i, j: integer;
begin
Result := false;
try
RegEx.subject := s;
i := -1;
j := length(sReplace);
while RegEx.MatchAgain do
begin
Inc(i);
if not(i > j - 1) then
begin
RegEx.Replacement := sReplace[i];
RegEx.Replace;
Result := true;
end
else
break;
end;
if Result and (i = j - 1) then
s := RegEx.subject;
except
end;
end;
function tregExUtil.regExExtractAliasStrict(const s: string): TArray<string>;
begin
Result := regExExtractValueEx(regExAliasStrict, s);
end;
function tregExUtil.regExExtractAlias(const s: string): TArray<string>;
begin
Result := regExExtractValueEx(regExAlias, s);
end;
function tregExUtil.regExExtractNumber(const s: string): TArray<string>;
begin
Result := regExExtractValueEx(regExNumber, s);
end;
function tregExUtil.regExExtractWords(const s: string): TArray<string>;
begin
Result := regExExtractValueEx(regExWord, s);
end;
function tregExUtil.regExExtractValues(const id: integer; const s: string): TArray<string>;
begin
case id of
1: Result := regExExtractNumber(s);
2: Result := regExExtractAlias(s);
3: Result := regExExtractAliasStrict(s);
else
setlength(Result, 0);
end;
end;
function tregExUtil.regExExtractValues(const rEx: string; const s: string): TArray<string>;
var
i: integer;
m: tmatch;
begin
Result := nil;
i := 0;
if length(s) = 0 then
exit;
try
m := TRegEx.match(s, rEx);
while m.Success do
begin
Inc(i);
setlength(Result, i);
Result[i - 1] := m.Value;
m := m.NextMatch;
end;
except
end;
end;
function tregExUtil.regExExtractValueEx(RegEx: tperlRegex; const s: string): TArray<string>;
var
i: integer;
begin
Result := nil;
i := 0;
if length(s) = 0 then
exit;
try
RegEx.subject := s;
while RegEx.MatchAgain do
begin
Inc(i);
setlength(Result, i);
Result[i - 1] := RegEx.MatchedText;
end;
except
end;
end;
function tregExUtil.regExNormalizeAlias(var s: string): boolean;
var
a: TArray<string>;
begin
a := regExExtractAliasStrict(s);
Result := length(a) > 0;
if Result then
regExReplaceValuesEx(regExAliasStrict, s, rxNormalizedAlias);
end;
procedure tregExUtil.regExNormalizeAlias(a: TArray<string>; var s: string);
begin
if length(a) > 0 then
regExReplaceValuesEx(regExAliasStrict, s, rxNormalizedAlias);
end;
function tregExUtil.regExNormalizeNumber(var s: string): boolean;
var
a: TArray<string>;
begin
a := regExExtractNumber(s);
Result := length(a) > 0;
if Result then
regExReplaceValuesEx(regExNumber, s, rxNormalizedNumber);
end;
function tregExUtil.regExReplaceValues(const pattern: string; var s: string; a: TArray<string>): boolean;
var
RegEx: tperlRegex;
i: integer;
begin
Result := false;
RegEx := tperlRegex.Create;
try
try
RegEx.Options := [];
RegEx.RegEx := pattern;
RegEx.subject := s;
i := -1;
while RegEx.MatchAgain do
begin
Inc(i);
if i > (length(a) - 1) then
begin
break;
Result := false;
end;
RegEx.Replacement := a[i];
RegEx.Replace;
Result := true;
end;
if Result then
s := RegEx.subject;
except
end;
finally
RegEx.free;
end;
end;
end.