-
Notifications
You must be signed in to change notification settings - Fork 8
/
pregexp-test.scm
407 lines (293 loc) · 8.69 KB
/
pregexp-test.scm
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
":";exec mzscheme -v -f pregexp.scm -f $0
;last substantial change 2005-04-24
;last change 2020-01-29
(load (if 'nil "tester.scm" "tester.lisp"))
;keeping the document honest
(test
(pregexp "c.r")
(:sub (:or (:seq #\c :any #\r)))
(pregexp-match-positions "brain" "bird")
#f
(pregexp-match-positions "needle" "hay needle stack")
((4 . 10))
(pregexp-match-positions "needle"
"his hay needle stack -- my hay needle stack -- her hay needle stack"
24 43)
((31 . 37))
(pregexp-match "brain" "bird")
#f
(pregexp-match "needle" "hay needle stack")
("needle")
(pregexp-split ":" "/bin:/usr/bin:/usr/bin/X11:/usr/local/bin")
("/bin" "/usr/bin" "/usr/bin/X11" "/usr/local/bin")
(pregexp-split " " "pea soup")
("pea" "soup")
(pregexp-split "" "smithereens")
("s" "m" "i" "t" "h" "e" "r" "e" "e" "n" "s")
(pregexp-split " +" "split pea soup")
("split" "pea" "soup")
(pregexp-split " *" "split pea soup")
("s" "p" "l" "i" "t" "p" "e" "a" "s" "o" "u" "p")
(pregexp-replace "te" "liberte" "ty")
"liberty"
(pregexp-replace* "te" "liberte egalite fraternite" "ty")
"liberty egality fratyrnity"
(pregexp-match-positions "^contact" "first contact")
#f
(pregexp-match-positions "laugh$" "laugh laugh laugh laugh")
((18 . 23))
(pregexp-match-positions "yack\\b" "yackety yack")
((8 . 12))
(pregexp-match-positions "an\\B" "an analysis")
((3 . 5))
(pregexp-match "p.t" "pet")
("pet")
(pregexp-match "\\d\\d" "0 dear, 1 have to read catch 22 before 9")
("22")
(pregexp-match "[[:alpha:]_]" "--x--")
("x")
(pregexp-match "[[:alpha:]_]" "--_--")
("_")
(pregexp-match "[[:alpha:]_]" "--:--")
#f
(pregexp-match "[:alpha:]" "--a--")
("a")
(pregexp-match "[:alpha:]" "--_--")
#f
(pregexp-match-positions "c[ad]*r" "cadaddadddr")
((0 . 11))
(pregexp-match-positions "c[ad]*r" "cr")
((0 . 2))
(pregexp-match-positions "c[ad]+r" "cadaddadddr")
((0 . 11))
(pregexp-match-positions "c[ad]+r" "cr")
#f
(pregexp-match-positions "c[ad]?r" "cadaddadddr")
#f
(pregexp-match-positions "c[ad]?r" "cr")
((0 . 2))
(pregexp-match-positions "c[ad]?r" "car")
((0 . 3))
(pregexp-match "[aeiou]{3}" "vacuous")
("uou")
(pregexp-match "[aeiou]{3}" "evolve")
#f
(pregexp-match "[aeiou]{2,3}" "evolve")
#f
(pregexp-match "[aeiou]{2,3}" "zeugma")
("eu")
(pregexp-match "<.*>" "<tag1> <tag2> <tag3>")
("<tag1> <tag2> <tag3>")
(pregexp-match "<.*?>" "<tag1> <tag2> <tag3>")
("<tag1>")
(pregexp-match "([a-z]+) ([0-9]+), ([0-9]+)" "jan 1, 1970")
("jan 1, 1970" "jan" "1" "1970")
(pregexp-match "(poo )*" "poo poo platter")
("poo poo " "poo ")
(pregexp-match "([a-z ]+;)*" "lather; rinse; repeat;")
("lather; rinse; repeat;" " repeat;")
)
(define date-re
;match `month year' or `month day, year'.
;subpattern matches day, if present
(pregexp "([a-z]+) +([0-9]+,)? *([0-9]+)"))
(test
(pregexp-match date-re "jan 1, 1970")
("jan 1, 1970" "jan" "1," "1970")
(pregexp-match date-re "jan 1970")
("jan 1970" "jan" #f "1970")
(pregexp-replace "_(.+?)_"
"the _nina_, the _pinta_, and the _santa maria_"
"*\\1*")
"the *nina*, the _pinta_, and the _santa maria_"
(pregexp-replace* "_(.+?)_"
"the _nina_, the _pinta_, and the _santa maria_"
"*\\1*")
"the *nina*, the *pinta*, and the *santa maria*"
(pregexp-replace "(\\S+) (\\S+) (\\S+)"
"eat to live"
"\\3 \\2 \\1")
"live to eat"
(pregexp-match "([a-z]+) and \\1"
"billions and billions")
("billions and billions" "billions")
(pregexp-match "([a-z]+) and \\1"
"billions and millions")
#f
(pregexp-replace* "(\\S+) \\1"
"now is the the time for all good men to to come to the aid of of the party"
"\\1")
"now is the time for all good men to come to the aid of the party"
(pregexp-replace* "(\\d+)\\1"
"123340983242432420980980234"
"{\\1,\\1}")
"12{3,3}40983{24,24}3242{098,098}0234"
(pregexp-match "^(?:[a-z]*/)*([a-z]+)$" "/usr/local/bin/mzscheme")
("/usr/local/bin/mzscheme" "mzscheme")
(pregexp-match "(?i:hearth)" "HeartH")
("HeartH")
(pregexp-match "(?x: a lot)" "alot")
("alot")
(pregexp-match "(?x: a \\ lot)" "a lot")
("a lot")
(pregexp-match "(?x:
a \\ man \\; \\ ; ignore
a \\ plan \\; \\ ; me
a \\ canal ; completely
)"
"a man; a plan; a canal")
("a man; a plan; a canal")
(pregexp-match "(?ix:
a \\ man \\; \\ ; ignore
a \\ plan \\; \\ ; me
a \\ canal ; completely
)"
"A Man; a Plan; a Canal")
("A Man; a Plan; a Canal")
(pregexp-match "(?i:the (?-i:TeX)book)"
"The TeXbook")
("The TeXbook")
(pregexp-match "f(ee|i|o|um)" "a small, final fee")
("fi" "i")
(pregexp-replace* "([yi])s(e[sdr]?|ing|ation)"
"it is energising to analyse an organisation pulsing with noisy organisms"
"\\1z\\2")
"it is energizing to analyze an organization pulsing with noisy organisms"
(pregexp-match "f(?:ee|i|o|um)" "fun for all")
("fo")
(pregexp-match "call|call-with-current-continuation"
"call-with-current-continuation")
("call")
(pregexp-match "call-with-current-continuation|call"
"call-with-current-continuation")
("call-with-current-continuation")
(pregexp-match "(?:call|call-with-current-continuation) constrained"
"call-with-current-continuation constrained")
("call-with-current-continuation constrained")
(pregexp-match "(?>a+)." "aaaa")
#f
(pregexp-match-positions "grey(?=hound)"
"i left my grey socks at the greyhound")
((28 . 32))
(pregexp-match-positions "grey(?!hound)"
"the gray greyhound ate the grey socks")
((27 . 31))
(pregexp-match-positions "(?<=grey)hound"
"the hound in the picture is not a greyhound")
((38 . 43))
(pregexp-match-positions "(?<!grey)hound"
"the greyhound in the picture is not a hound")
((38 . 43))
(pregexp-match-positions "(?<=grey)hound"
"\nthe hound in the picture is not a greyhound")
((39 . 44))
(pregexp-match-positions "(?<!grey)hound"
"\nthe greyhound in the picture is not a hound")
((39 . 44))
(pregexp-match-positions "(?<=grey)hound" "greyhound" 4)
#f
(pregexp-match-positions "(?<!grey)hound" "greyhound" 4)
((4 . 9))
)
(define n0-255
"(?x:
\\d ; 0 through 9
| \\d\\d ; 00 through 99
| [01]\\d\\d ;000 through 199
| 2[0-4]\\d ;200 through 249
| 25[0-5] ;250 through 255
)")
(define ip-re1
(string-append
"^" ;nothing before
n0-255 ;the first n0-255,
"(?x:" ;then the subpattern of
"\\." ;a dot followed by
n0-255 ;an n0-255,
")" ;which is
"{3}" ;repeated exactly 3 times
"$" ;with nothing following
))
(test
(pregexp-match ip-re1
"1.2.3.4")
("1.2.3.4")
(pregexp-match ip-re1
"55.155.255.265")
#f
(pregexp-match ip-re1
"0.00.000.00")
("0.00.000.00")
)
(define ip-re
(string-append
"(?=[1-9])" ;ensure there's a non-0 digit
ip-re1))
(test
(pregexp-match ip-re
"1.2.3.4")
("1.2.3.4")
(pregexp-match ip-re
"0.0.0.0")
#f
)
(set! ip-re
(string-append
"(?![0.]*$)" ;not just zeros and dots
;dot is not metachar inside []
ip-re1))
(test
(pregexp-match ip-re
"1.2.3.4")
("1.2.3.4")
(pregexp-match ip-re
"0.0.0.0")
#f
;misc
(pregexp-match "a[^a]*b" "glauber")
("aub")
(pregexp-match "a([^a]*)b" "glauber")
("aub" "u")
(pregexp-match "a([^a]*)b" "ababababab")
("ab" "")
(pregexp-match "(?x: s e * k )" "seeeeek")
("seeeeek")
(pregexp-match "(?x: t ;matches t
h ; matches h
e ;;; matches e
\\ ; ; ; matches space
\\; ; matches ;
)"
"the ;")
("the ;")
(pregexp-replace* "^(.*)$" "foobar" "\\1abc")
"foobarabc"
(pregexp-replace* "^(.*)$" "foobar" "abc\\1")
"abcfoobar"
(pregexp-replace* "(.*)$" "foobar" "abc\\1")
"abcfoobar"
)
(test
;PLT bug 6095 from Neil W. Van Dyke
(pregexp "[a-z-]")
(:sub (:or (:seq (:one-of-chars (:char-range #\a #\z) #\-))))
;
(pregexp "[-a-z]")
(:sub (:or (:seq (:one-of-chars #\- (:char-range #\a #\z)))))
;PLT bug 6442 from David T. Pierson
(pregexp-match-positions "(a(b))?c" "abc")
((0 . 3) (0 . 2) (1 . 2))
;
(pregexp-match-positions "(a(b))?c" "c")
((0 . 1) #f #f)
;PLT bug 7233 from Edi Weitz
(length (pregexp-match "(a)|(b)" "b"))
3
;PLT bug 7232 from Neil Van Dyke
(pregexp "[-a]")
(:sub (:or (:seq (:one-of-chars #\- #\a))))
;
(pregexp "[a-]")
(:sub (:or (:seq (:one-of-chars #\a #\-))))
)
(bottomline)