-
Notifications
You must be signed in to change notification settings - Fork 0
/
irregex.html
1164 lines (885 loc) · 65.3 KB
/
irregex.html
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html><head><title>IrRegular Expressions</title>
</head><body bgcolor=white>
<h1><span class=subject>IrRegular Expressions</span></h1>
<style>
body {
color: black;
background-color: white;
margin-top: 2em;
margin-left: 10%;
width: 400pt;
}
pre {
background-color: beige;
}
pre.scheme {
background-color: white;
}
blockquote {
color: green;
font-size: small;
}
.subject {
}
h1 {
margin-left: -5%;
margin-top: 2em;
font-size: large;
}
h2 {
margin-left: -4%;
margin-top: 1em;
font-size: large;
}
h3,h4,h5,h6 {
margin-left: -3%;
margin-top: .5em;
font-size: small;
}
.navigation {
color: red;
background-color: beige;
text-align: right;
font-style: italic;
}
.scheme {
color: brown;
}
.scheme .keyword {
color: #cc0000;
font-weight: bold;
}
.scheme .variable {
color: navy;
}
.scheme .global {
color: purple;
}
.scheme .constant,.number,.char,.string,.boolean {
color: green;
}
.scheme .comment {
color: teal;
}
</style>
<div align=right><a href="http://synthcode.com/">Alex Shinn</a></div>
<div align=right><a href="http://synthcode.com/scheme/irregex/irregex-0.9.4.tar.gz">Download Version 0.9.4</a></div>
<p>
<br /><br />
<blockquote>At this moment there was a loud ring at the bell, and I could
hear Mrs. Hudson, our landlady, raising her voice in a wail of
expostulation and dismay.
<p>
"By heaven, Holmes," I said, half rising, "I believe that
they are really after us."
<p>
"No, it's not quite so bad as that. It is the unofficial
force, -- the Baker Street irregulars."
<p>
</blockquote>
<br /><br />
A fully portable and efficient R[4567]RS implementation of regular
expressions, supporting both POSIX syntax with various (irregular)
PCRE extensions, as well as SCSH's SRE syntax, with various aliases
for commonly used patterns. DFA matching is used when possible,
otherwise a closure-compiled NFA approach is used. The library makes
no assumptions about the encoding of strings or range of characters
and can thus be used in Unicode-aware Scheme implementations.
Matching may be performed over standard Scheme strings, or over
arbitrarily chunked streams of strings.
<p>
<br /><br />
<a name="SECTION_1"><h1>1 Table of Contents</h1>
<ol>
<li><a href="#SECTION_1">Table of Contents</a>
<li><a href="#SECTION_2">Installation</a>
<li><a href="#SECTION_3">Specification</a>
<ol>
<li><a href="#SECTION_3.1">Procedures</a>
<li><a href="#SECTION_3.2">Extended SRE Syntax</a>
<a name="SECTION_3.2.1"><h3>3.2.1 Basic SRE Patterns</h3>
<a name="SECTION_3.2.2"><h3>3.2.2 SRE Repetition Patterns</h3>
<a name="SECTION_3.2.3"><h3>3.2.3 SRE Character Sets</h3>
<a name="SECTION_3.2.4"><h3>3.2.4 SRE Assertion Patterns</h3>
<a name="SECTION_3.2.5"><h3>3.2.5 SRE Utility Patterns</h3>
<li><a href="#SECTION_3.3">Supported PCRE Syntax</a>
<li><a href="#SECTION_3.4">Chunked String Matching</a>
<li><a href="#SECTION_3.5">Utilities</a>
</ol>
<li><a href="#SECTION_4">Roadmap</a>
<li><a href="#SECTION_5">License</a>
<li><a href="#SECTION_6">References</a>
</ol>
<br /><br />
<a name="SECTION_2"><h1>2 Installation</h1>
Just
<p>
<pre class=scheme>
(<span class=variable>load</span> <span class=string>"irregex.scm"</span>)
</pre>
<p>
in your favorite Scheme implementation and you're good to go!
<p>
There is a global variable <code class=scheme><span class=global>*all-chars*</span></code> which is used for
generating character set complements. This defaults to the full
Unicode range 0..#x10FFFF, but if your implementation can't handle
characters that large you'll need to adjust it (a suitable ASCII
definition is commented out in the source).
<p>
If you are using an R6RS Scheme, you can instead
<p>
<pre class=scheme>
(<span class=variable>load</span> <span class=string>"irregex-r6rs.scm"</span>)
</pre>
<p>
There are also a handful of utility procedures described below you may
wish to use in irregex-utils.scm.
<p>
If you are using Chicken Scheme IrRegex is built in as a core unit, so
no need to install it. To use it, you just need to <code class=scheme>(<span class=variable>use</span> <span class=variable>irregex</span>)</code>.
<p>
<a name="SECTION_3"><h1>3 Specification</h1>
<a name="SECTION_3.1"><h2>3.1 Procedures</h2>
<h3>(irregex <posix-string-or-sre> [<options> ...])</h3>
<h3>(string->irregex <posix-string> [<options> ...])</h3>
<h3>(sre->irregex <sre> [<options> ...])</h3>
Compiles a regular expression from either a POSIX-style regular
expression string (with most PCRE extensions) or an SCSH-style SRE.
There is no <code class=scheme>(<span class=variable>rx</span> ...)</code> syntax - just use normal Scheme lists, with
<code class=scheme><span class=keyword>quasiquote</span></code> if you like.
<p>
Technically a string by itself could be considered a valid (though
rather silly) SRE, so if you want to just match a literal string you
should use something like <code class=scheme>(<span class=variable>irregex</span> `(<span class=constant>:</span> ,<span class=variable>str</span>))</code>, or use the explicit
<code class=scheme>(<span class=variable>sre->irregex</span> <span class=variable>str</span>)</code>.
<p>
The options are a list of any of the following symbols:
<p>
<code class=scheme>'<span class=variable>i</span></code>, <code class=scheme>'<span class=variable>case-insensitive</span></code> - match case-insensitively
<p>
<code class=scheme>'<span class=variable>m</span></code>, <code class=scheme>'<span class=variable>multi-line</span></code> - treat string as multiple lines (effects ^ and $)
<p>
<code class=scheme>'<span class=variable>s</span></code>, <code class=scheme>'<span class=variable>single-line</span></code> - treat string as a single line (. can match newline)
<p>
<code class=scheme>'<span class=variable>utf8</span></code> - utf8-mode (assumes strings are byte-strings)
<p>
<code class=scheme>'<span class=variable>fast</span></code> - try to optimize the regular expression
<p>
<code class=scheme>'<span class=variable>small</span></code> - try to compile a smaller regular expression
<p>
<code class=scheme>'<span class=variable>backtrack</span></code> - enforce a backtracking implementation
<p>
The <code class=scheme>'<span class=variable>fast</span></code> and <code class=scheme>'<span class=variable>small</span></code> options are heuristic guidelines and will
not necessarily make the compiled expression faster or smaller.
<p>
<h3>(string->sre <str>)</h3>
<h3>(maybe-string->sre <obj>)</h3>
For backwards compatibility, procedures to convert a POSIX string into
an SRE.
<p>
<code class=scheme><span class=variable>maybe-string->sre</span></code> does the same thing, but only if the argument is
a string, otherwise it assumes <code class=scheme><span class=variable><obj></span></code> is an SRE and returns it
as-is. This is useful when you want to provide an API that allows
either a POSIX string or SRE (like <code class=scheme><span class=variable>irregex</span></code> or <code class=scheme><span class=variable>irregex-search</span></code>
below) - it ensures the result is an SRE.
<p>
<h3>(irregex? <obj>)</h3>
Returns <code class=scheme><span class=boolean>#t</span></code> iff the object is a regular expression.
<p>
<h3>(irregex-search <irx> <str> [<start> <end>])</h3>
Searches for any instances of the pattern <irx> (a POSIX string, SRE
sexp, or pre-compiled regular expression) in <str>, optionally between
the given range. If a match is found, returns a match object,
otherwise returns <code class=scheme><span class=boolean>#f</span></code>.
<p>
Match objects can be used to query the original range of the string or
its submatches using the <code class=scheme><span class=variable>irregex-match-*</span></code> procedures below.
<p>
Examples:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> <span class=string>"foobar"</span> <span class=string>"abcFOOBARdef"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> (<span class=variable>irregex</span> <span class=string>"foobar"</span> '<span class=variable>i</span>) <span class=string>"abcFOOBARdef"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=variable>w/nocase</span> <span class=string>"foobar"</span>) <span class=string>"abcFOOBARdef"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
Note, the actual match result is represented by a vector in the
default implementation. Throughout this document, we'll just write
<code class=scheme><span class=variable><match></span></code> to show that a successful match was returned when the
details are not important.
<p>
Matching follows the POSIX leftmost, longest semantics, when
searching. That is, of all possible matches in the string,
<code class=scheme><span class=variable>irregex-search</span></code> will return the match at the first position
(leftmost). If multiple matches are possible from that same first
position, the longest match is returned.
<p>
<h3>(irregex-match <irx> <str> [<start> <end>])</h3>
Like <code class=scheme><span class=variable>irregex-search</span></code>, but performs an anchored match against the
beginning and end of the substring specified by <start> and <end>,
without searching.
<p>
Examples:
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=variable>w/nocase</span> <span class=string>"foobar"</span>) <span class=string>"abcFOOBARdef"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=variable>w/nocase</span> <span class=string>"foobar"</span>) <span class=string>"FOOBAR"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<h3>(irregex-match-data? <obj>)</h3>
Returns <code class=scheme><span class=boolean>#t</span></code> iff the object is a successful match result from
<code class=scheme><span class=variable>irregex-search</span></code> or <code class=scheme><span class=variable>irregex-match</span></code>.
<p>
<h3>(irregex-num-submatches <irx>)</h3>
<h3>(irregex-match-num-submatches <match>)</h3>
Returns the number of numbered submatches that are defined in the
irregex or match object.
<p>
<h3>(irregex-names <irx>)</h3>
<h3>(irregex-match-names <match>)</h3>
Returns an association list of named submatches that are defined in
the irregex or match object. The <code class=scheme><span class=variable>car</span></code> of each item in this list is
the name of a submatch, the <code class=scheme><span class=variable>cdr</span></code> of each item is the numerical
submatch corresponding to this name. If a named submatch occurs
multiple times in the irregex, it will also occur multiple times in
this list.
<p>
<h3>(irregex-match-valid-index? <match> <index-or-name>)</h3>
Returns <code class=scheme><span class=boolean>#t</span></code> iff the <code class=scheme><span class=variable>index-or-name</span></code> named submatch or index is
defined in the <code class=scheme><span class=variable>match</span></code> object.
<p>
<h3>(irregex-match-substring <match> [<index-or-name>])</h3>
<h3>(irregex-match-start-index <match> [<index-or-name>])</h3>
<h3>(irregex-match-end-index <match> [<index-or-name>])</h3>
Fetches the matched substring (or its start or end offset) at the
given submatch index, or named submatch. The entire match is index 0,
the first 1, etc. The default is index 0.
<p>
<h3>(irregex-match-subchunk <match> [<index-or-name>])</h3>
Generates a chunked data-type for the given match item, of the same
type as the underlying chunk type (see Chunked String Matching below).
This is only available if the chunk type specifies the get-subchunk
API, otherwise an error is raised.
<p>
<h3>(irregex-replace <irx> <str> [<replacements> ...])</h3>
<h3>(irregex-replace/all <irx> <str> [<replacements> ...])</h3>
Matches a pattern in a string, and replaces it with a (possibly empty)
list of substitutions. Each <code class=scheme><span class=variable><replacement></span></code> can be either a string
literal, a numeric index, a symbol (as a named submatch), or a
procedure which takes one argument (the match object) and returns a
string.
<p>
Examples:
<p>
<code class=scheme>(<span class=variable>irregex-replace</span> <span class=string>"[aeiou]"</span> <span class=string>"hello world"</span> <span class=string>"*"</span>) <span class=keyword>=></span> <span class=string>"h*llo world"</span></code>
<p>
<code class=scheme>(<span class=variable>irregex-replace/all</span> <span class=string>"[aeiou]"</span> <span class=string>"hello world"</span> <span class=string>"*"</span>) <span class=keyword>=></span> <span class=string>"h*ll* w*rld"</span></code>
<p>
<h3>(irregex-split <irx> <str> [<start> <end>])</h3>
<h3>(irregex-extract <irx> <str> [<start> <end>])</h3>
<code class=scheme><span class=variable>irregex-split</span></code> splits the string <code class=scheme><span class=variable><str></span></code> into substrings divided
by the pattern in <code class=scheme><span class=variable><irx></span></code>. <code class=scheme><span class=variable>irregex-extract</span></code> does the opposite,
returning a list of each instance of the pattern matched disregarding
the substrings in between.
<p>
<h3>(irregex-fold <irx> <kons> <knil> <str> [<finish> <start> <end>])</h3>
This performs a fold operation over every non-overlapping place
<code class=scheme><span class=variable><irx></span></code> occurs in the string <code class=scheme><span class=variable>str</span></code>.
<p>
The <code class=scheme><span class=variable><kons></span></code> procedure takes the following signature:
<p>
<code class=scheme>(<span class=variable><kons></span> <span class=variable><from-index></span> <span class=variable><match></span> <span class=variable><seed></span>)</code>
<p>
where <code class=scheme><span class=variable><from-index></span></code> is the index from where we started searching
(initially <code class=scheme><span class=variable><start></span></code> and thereafter the end index of the last
match), <code class=scheme><span class=variable><match></span></code> is the resulting match-data object, and <code class=scheme><span class=variable><seed></span></code>
is the accumulated fold result starting with <code class=scheme><span class=variable><knil></span></code>.
<p>
The rationale for providing the <code class=scheme><span class=variable><from-index></span></code> (which is not
provided in the SCSH <code class=scheme><span class=variable>regexp-fold</span></code> utility), is because this
information is useful (e.g. for extracting the unmatched portion of
the string before the current match, as needed in
<code class=scheme><span class=variable>irregex-replace</span></code>), and not otherwise directly accessible.
<p>
The optional <code class=scheme><span class=variable><finish></span></code> takes two arguments:
<p>
<code class=scheme>(<span class=variable><finish></span> <span class=variable><from-index></span> <span class=variable><seed></span>)</code>
<p>
which simiarly allows you to pick up the unmatched tail of the string,
and defaults to just returning the <code class=scheme><span class=variable><seed></span></code>.
<p>
<code class=scheme><span class=variable><start></span></code> and <code class=scheme><span class=variable><end></span></code> are numeric indices letting you specify the
boundaries of the string on which you want to fold.
<p>
To extract all instances of a match out of a string, you can use
<p>
<code class=scheme>(<span class=variable>map</span> <span class=variable>irregex-match-substring</span> (<span class=variable>irregex-fold</span> <span class=variable><irx></span> (<span class=keyword>lambda</span> (<span class=variable>i</span> <span class=variable>m</span> <span class=variable>s</span>) (<span class=variable>cons</span> <span class=variable>m</span> <span class=variable>s</span>)) '() <span class=variable><str></span> (<span class=keyword>lambda</span> (<span class=variable>i</span> <span class=variable>s</span>) (<span class=variable>reverse</span> <span class=variable>s</span>))))</code>
<p>
<a name="SECTION_3.2"><h2>3.2 Extended SRE Syntax</h2>
Irregex provides the first native implementation of SREs (Scheme
Regular Expressions), and includes many extensions necessary both for
minimal POSIX compatibility, as well as for modern extensions found in
libraries such as PCRE.
<p>
The following table summarizes the SRE syntax, with detailed
explanations following.
<p>
<pre class=scheme>
<span class=comment>;; basic patterns</span>
<span class=variable><string></span> <span class=comment>; literal string</span>
(<span class=variable>seq</span> <span class=variable><sre></span> ...) <span class=comment>; sequence</span>
(<span class=constant>:</span> <span class=variable><sre></span> ...)
(<span class=keyword>or</span> <span class=variable><sre></span> ...) <span class=comment>; alternation</span>
<span class=comment>;; optional/multiple patterns</span>
(<span class=variable>?</span> <span class=variable><sre></span> ...) <span class=comment>; 0 or 1 matches</span>
(<span class=global>*</span> <span class=variable><sre></span> ...) <span class=comment>; 0 or more matches</span>
(<span class=variable>+</span> <span class=variable><sre></span> ...) <span class=comment>; 1 or more matches</span>
(<span class=variable>=</span> <span class=variable><n></span> <span class=variable><sre></span> ...) <span class=comment>; exactly <n> matches</span>
(<span class=variable>>=</span> <span class=variable><n></span> <span class=variable><sre></span> ...) <span class=comment>; <n> or more matches</span>
(<span class=global>**</span> <span class=variable><from></span> <span class=variable><to></span> <span class=variable><sre></span> ...) <span class=comment>; <n> to <m> matches</span>
(<span class=variable>??</span> <span class=variable><sre></span> ...) <span class=comment>; non-greedy (non-greedy) pattern: (0 or 1)</span>
(<span class=variable>*?</span> <span class=variable><sre></span> ...) <span class=comment>; non-greedy kleene star</span>
(<span class=variable>**?</span> <span class=variable><from></span> <span class=variable><to></span> <span class=variable><sre></span> ...) <span class=comment>; non-greedy range</span>
<span class=comment>;; submatch patterns</span>
(<span class=variable>submatch</span> <span class=variable><sre></span> ...) <span class=comment>; numbered submatch</span>
(<span class=variable>$</span> <span class=variable><sre></span> ...)
(<span class=variable>submatch-named</span> <span class=variable><name></span> <span class=variable><sre></span> ...) <span class=comment>; named submatch</span>
(<span class=keyword>=></span> <span class=variable><name></span> <span class=variable><sre></span> ...)
(<span class=variable>backref</span> <span class=variable><n-or-name></span>) <span class=comment>; match a previous submatch</span>
<span class=comment>;; toggling case-sensitivity</span>
(<span class=variable>w/case</span> <span class=variable><sre></span> ...) <span class=comment>; enclosed <sre>s are case-sensitive</span>
(<span class=variable>w/nocase</span> <span class=variable><sre></span> ...) <span class=comment>; enclosed <sre>s are case-insensitive</span>
<span class=comment>;; character sets</span>
<span class=variable><char></span> <span class=comment>; singleton char set</span>
(<span class=variable><string></span>) <span class=comment>; set of chars</span>
(<span class=keyword>or</span> <span class=variable><cset-sre></span> ...) <span class=comment>; set union</span>
(<span class=variable>~</span> <span class=variable><cset-sre></span> ...) <span class=comment>; set complement (i.e. [^...])</span>
(<span class=variable>-</span> <span class=variable><cset-sre></span> ...) <span class=comment>; set difference</span>
(<span class=variable>&</span> <span class=variable><cset-sre></span> ...) <span class=comment>; set intersection</span>
(<span class=variable>/</span> <span class=variable><range-spec></span> ...) <span class=comment>; pairs of chars as ranges</span>
<span class=comment>;; named character sets</span>
<span class=variable>any</span>
<span class=variable>nonl</span>
<span class=variable>ascii</span>
<span class=variable>lower-case</span> <span class=variable>lower</span>
<span class=variable>upper-case</span> <span class=variable>upper</span>
<span class=variable>alphabetic</span> <span class=variable>alpha</span>
<span class=variable>numeric</span> <span class=variable>num</span>
<span class=variable>alphanumeric</span> <span class=variable>alphanum</span> <span class=variable>alnum</span>
<span class=variable>punctuation</span> <span class=variable>punct</span>
<span class=variable>graphic</span> <span class=variable>graph</span>
<span class=variable>whitespace</span> <span class=variable>white</span> <span class=variable>space</span>
<span class=variable>printing</span> <span class=variable>print</span>
<span class=variable>control</span> <span class=variable>cntrl</span>
<span class=variable>hex-digit</span> <span class=variable>xdigit</span>
<span class=comment>;; assertions and conditionals</span>
<span class=variable>bos</span> <span class=variable>eos</span> <span class=comment>; beginning/end of string</span>
<span class=variable>bol</span> <span class=variable>eol</span> <span class=comment>; beginning/end of line</span>
<span class=variable>bow</span> <span class=variable>eow</span> <span class=comment>; beginning/end of word</span>
<span class=variable>nwb</span> <span class=comment>; non-word-boundary</span>
(<span class=variable>look-ahead</span> <span class=variable><sre></span> ...) <span class=comment>; zero-width look-ahead assertion</span>
(<span class=variable>look-behind</span> <span class=variable><sre></span> ...) <span class=comment>; zero-width look-behind assertion</span>
(<span class=variable>neg-look-ahead</span> <span class=variable><sre></span> ...) <span class=comment>; zero-width negative look-ahead assertion</span>
(<span class=variable>neg-look-behind</span> <span class=variable><sre></span> ...) <span class=comment>; zero-width negative look-behind assertion</span>
(<span class=variable>atomic</span> <span class=variable><sre></span> ...) <span class=comment>; for (?>...) independent patterns</span>
(<span class=keyword>if</span> <span class=variable><test></span> <span class=variable><pass></span> [<span class=variable><fail></span>]) <span class=comment>; conditional patterns</span>
<span class=variable>commit</span> <span class=comment>; don't backtrack beyond this (i.e. cut)</span>
<span class=comment>;; backwards compatibility</span>
(<span class=variable>posix-string</span> <span class=variable><string></span>) <span class=comment>; embed a POSIX string literal</span>
</pre>
<p>
<a name="SECTION_3.2.1"><h3>3.2.1 Basic SRE Patterns</h3>
The simplest SRE is a literal string, which matches that string
exactly.
<p>
<code class=scheme>(<span class=variable>irregex-search</span> <span class=string>"needle"</span> <span class=string>"hayneedlehay"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
By default the match is case-sensitive, though you can control this
either with the compiler flags or local overrides:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> <span class=string>"needle"</span> <span class=string>"haynEEdlehay"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> (<span class=variable>irregex</span> <span class=string>"needle"</span> '<span class=variable>i</span>) <span class=string>"haynEEdlehay"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=variable>w/nocase</span> <span class=string>"needle"</span>) <span class=string>"haynEEdlehay"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
You can use <code class=scheme><span class=variable>w/case</span></code> to switch back to case-sensitivity inside a
<code class=scheme><span class=variable>w/nocase</span></code> or when the SRE was compiled with <code class=scheme>'<span class=variable>i</span></code>:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=variable>w/nocase</span> <span class=string>"SMALL"</span> (<span class=variable>w/case</span> <span class=string>"BIG"</span>)) <span class=string>"smallBIGsmall"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=variable>w/nocase</span> <span class=string>"small"</span> (<span class=variable>w/case</span> <span class=string>"big"</span>)) <span class=string>"smallBIGsmall"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
<strong>Important:</strong> characters outside the ASCII range are only matched
case insensitively if the host Scheme system natively supports UTF8 in
strings.
<p>
Of course, literal strings by themselves aren't very interesting
regular expressions, so we want to be able to compose them. The most
basic way to do this is with the <code class=scheme><span class=variable>seq</span></code> operator (or its abbreviation
<code class=scheme><span class=constant>:</span></code>), which matches one or more patterns consecutively:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"one"</span> <span class=variable>space</span> <span class=string>"two"</span> <span class=variable>space</span> <span class=string>"three"</span>) <span class=string>"one two three"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
As you may have noticed above, the <code class=scheme><span class=variable>w/case</span></code> and <code class=scheme><span class=variable>w/nocase</span></code>
operators allowed multiple SREs in a sequence - other operators that
take any number of arguments (e.g. the repetition operators below)
allow such implicit sequences.
<p>
To match any one of a set of patterns use the <code class=scheme><span class=keyword>or</span></code> alternation
operator:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=keyword>or</span> <span class=string>"eeney"</span> <span class=string>"meeney"</span> <span class=string>"miney"</span>) <span class=string>"meeney"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=keyword>or</span> <span class=string>"eeney"</span> <span class=string>"meeney"</span> <span class=string>"miney"</span>) <span class=string>"moe"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
<a name="SECTION_3.2.2"><h3>3.2.2 SRE Repetition Patterns</h3>
There are also several ways to control the number of times a pattern
is matched. The simplest of these is <code class=scheme><span class=variable>?</span></code> which just optionally
matches the pattern:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"match"</span> (<span class=variable>?</span> <span class=string>"es"</span>) <span class=string>"!"</span>) <span class=string>"matches!"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"match"</span> (<span class=variable>?</span> <span class=string>"es"</span>) <span class=string>"!"</span>) <span class=string>"match!"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"match"</span> (<span class=variable>?</span> <span class=string>"es"</span>) <span class=string>"!"</span>) <span class=string>"matche!"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
To optionally match any number of times, use <code class=scheme><span class=global>*</span></code>, the Kleene star:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=global>*</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<html>"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=global>*</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<>"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=global>*</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<html"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
Often you want to match any number of times, but at least one time is
required, and for that you use <code class=scheme><span class=variable>+</span></code>:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=variable>+</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<html>"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=variable>+</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<a>"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=variable>+</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<>"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
More generally, to match at least a given number of times, use <code class=scheme><span class=variable>>=</span></code>:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=variable>>=</span> <span class=number>3</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<table>"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=variable>>=</span> <span class=number>3</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<pre>"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=variable>>=</span> <span class=number>3</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<tr>"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
To match a specific number of times exactly, use <code class=scheme><span class=variable>=</span></code>:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=variable>=</span> <span class=number>4</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<html>"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"<"</span> (<span class=variable>=</span> <span class=number>4</span> (<span class=variable>~</span> <span class=char>#\></span>)) <span class=string>">"</span>) <span class=string>"<table>"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
And finally, the most general form is <code class=scheme><span class=global>**</span></code> which specifies a range
of times to match. All of the earlier forms are special cases of this.
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> (<span class=variable>=</span> <span class=number>3</span> (<span class=global>**</span> <span class=number>1</span> <span class=number>3</span> <span class=variable>numeric</span>) <span class=string>"."</span>) (<span class=global>**</span> <span class=number>1</span> <span class=number>3</span> <span class=variable>numeric</span>)) <span class=string>"192.168.1.10"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> (<span class=variable>=</span> <span class=number>3</span> (<span class=global>**</span> <span class=number>1</span> <span class=number>3</span> <span class=variable>numeric</span>) <span class=string>"."</span>) (<span class=global>**</span> <span class=number>1</span> <span class=number>3</span> <span class=variable>numeric</span>)) <span class=string>"192.0168.1.10"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
There are also so-called "non-greedy" variants of these repetition
operators, by convention suffixed with an additional <code class=scheme><span class=variable>?</span></code>. Since the
normal repetition patterns can match any of the allotted repetition
range, these operators will match a string if and only if the normal
versions matched. However, when the endpoints of which submatch
matched where are taken into account (specifically, all matches when
using irregex-search since the endpoints of the match itself matter),
the use of a non-greedy repetition can change the result.
<p>
So, whereas <code class=scheme><span class=variable>?</span></code> can be thought to mean "match or don't match,"
<code class=scheme><span class=variable>??</span></code> means "don't match or match." <code class=scheme><span class=global>*</span></code> typically consumes as much
as possible, but <code class=scheme><span class=variable>*?</span></code> tries first to match zero times, and only
consumes one at a time if that fails. If you have a greedy operator
followed by a non-greedy operator in the same pattern, they can
produce surprisins results as they compete to make the match longer or
shorter. If this seems confusing, that's because it is. Non-greedy
repetitions are defined only in terms of the specific backtracking
algorithm used to implement them, which for compatibility purposes
always means the Perl algorithm. Thus, when using these patterns you
force IrRegex to use a backtracking engine, and can't rely on
efficient execution.
<p>
<a name="SECTION_3.2.3"><h3>3.2.3 SRE Character Sets</h3>
Perhaps more common than matching specific strings is matching any of
a set of characters. You can use the <code class=scheme><span class=keyword>or</span></code> alternation pattern on a
list of single-character strings to simulate a character set, but this
is too clumsy for everyday use so SRE syntax allows a number of
shortcuts.
<p>
A single character matches that character literally, a trivial
character class. More conveniently, a list holding a single element
which is a string refers to the character set composed of every
character in the string.
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> <span class=char>#\-</span>) <span class=string>"---"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> <span class=char>#\-</span>) <span class=string>"-_-"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> (<span class=string>"aeiou"</span>)) <span class=string>"oui"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> (<span class=string>"aeiou"</span>)) <span class=string>"ouais"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
Ranges are introduced with the <code class=scheme><span class=variable>/</span></code> operator. Any strings or
characters in the <code class=scheme><span class=variable>/</span></code> are flattened and then taken in pairs to
represent the start and end points, inclusive, of character ranges.
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> (<span class=variable>/</span> <span class=string>"AZ09"</span>)) <span class=string>"R2D2"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> (<span class=variable>/</span> <span class=string>"AZ09"</span>)) <span class=string>"C-3PO"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
In addition, a number of set algebra operations are provided. <code class=scheme><span class=keyword>or</span></code>,
of course, has the same meaning, but when all the options are
character sets it can be thought of as the set union operator. This
is further extended by the <code class=scheme><span class=variable>&</span></code> set intersection, <code class=scheme><span class=variable>-</span></code> set
difference, and <code class=scheme><span class=variable>~</span></code> set complement operators.
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> (<span class=variable>&</span> (<span class=variable>/</span> <span class=string>"az"</span>) (<span class=variable>~</span> (<span class=string>"aeiou"</span>)))) <span class=string>"xyzzy"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> (<span class=variable>&</span> (<span class=variable>/</span> <span class=string>"az"</span>) (<span class=variable>~</span> (<span class=string>"aeiou"</span>)))) <span class=string>"vowels"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> (<span class=variable>-</span> (<span class=variable>/</span> <span class=string>"az"</span>) (<span class=string>"aeiou"</span>))) <span class=string>"xyzzy"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-match</span> '(<span class=global>*</span> (<span class=variable>-</span> (<span class=variable>/</span> <span class=string>"az"</span>) (<span class=string>"aeiou"</span>))) <span class=string>"vowels"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
<a name="SECTION_3.2.4"><h3>3.2.4 SRE Assertion Patterns</h3>
There are a number of times it can be useful to assert something about
the area around a pattern without explicitly making it part of the
pattern. The most common cases are specifically anchoring some
pattern to the beginning or end of a word or line or even the whole
string. For example, to match on the end of a word:
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"foo"</span> <span class=variable>eow</span>) <span class=string>"foo"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"foo"</span> <span class=variable>eow</span>) <span class=string>"foo!"</span>) <span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"foo"</span> <span class=variable>eow</span>) <span class=string>"foof"</span>) <span class=keyword>=></span> <span class=boolean>#f</span></code>
<p>
The <code class=scheme><span class=variable>bow</span></code>, <code class=scheme><span class=variable>bol</span></code>, <code class=scheme><span class=variable>eol</span></code>, <code class=scheme><span class=variable>bos</span></code> and <code class=scheme><span class=variable>eos</span></code> work similarly.
<code class=scheme><span class=variable>nwb</span></code> asserts that you are not in a word-boundary - if replaced for
<code class=scheme><span class=variable>eow</span></code> in the above examples it would reverse all the results.
<p>
There is no <code class=scheme><span class=variable>wb</span></code>, since you tend to know from context whether it
would be the beginning or end of a word, but if you need it you can
always use <code class=scheme>(<span class=keyword>or</span> <span class=variable>bow</span> <span class=variable>eow</span>)</code>.
<p>
Somewhat more generally, Perl introduced positive and negative
look-ahead and look-behind patterns. Perl look-behind patterns are
limited to a fixed length, however the IrRegex versions have no such
limit.
<p>
<code class=scheme>(<span class=variable>irregex-search</span> '(<span class=constant>:</span> <span class=string>"regular"</span> (<span class=variable>look-ahead</span> <span class=string>" expression"</span>))
<span class=string>"regular expression"</span>)
<span class=keyword>=></span> <span class=constant>#<match></span></code>
<p>
The most general case, of course, would be an <code class=scheme><span class=keyword>and</span></code> pattern to
complement the <code class=scheme><span class=keyword>or</span></code> pattern - all the patterns must match or the
whole pattern fails. This may be provided in a future release,
although it (and look-ahead and look-behind assertions) are unlikely
to be compiled efficiently.
<p>
<a name="SECTION_3.2.5"><h3>3.2.5 SRE Utility Patterns</h3>
The following utility regular expressions are also provided for common
patterns that people are eternally reinventing. They are not
necessarily the official patterns matching the RFC definitions of the
given data, because of the way that such patterns tend to be used.
There are three general usages for regexps:
<p>
<h4>searching</h4>
- search for a pattern matching a desired object in a larger text
<p>
<h4>validation</h4>
- determine whether an entire string matches a pattern
<p>
<h4>extraction</h4>
- given a string already known to be valid, extract certain fields from it as submatches
<p>
In some cases, but not always, these will overlap. When they are
different, <code class=scheme><span class=variable>irregex-search</span></code> will naturally always want the searching
version, so IrRegex provides that version.
<p>
As an example where these might be different, consider a URL. If you
want to match all the URLs in some arbitrary text, you probably want
to exclude a period or comma at the tail end of a URL, since it's more
likely being used as punctuation rather than part of the URL, despite
the fact that it would be valid URL syntax.
<p>
Another problem with the RFC definitions is the standard itself may
have become irrelevant. For example, the pattern IrRegex provides for
email addresses doesn't match quoted local parts (e.g. "first
last"@domain.com) because these are increasingly rare, and unsupported
by enough software that it's better to discourage their use.
Conversely, technically consecutive periods
(e.g. first..last@domain.com) are not allowed in email addresses, but
most email software does allow this, and in fact such addresses are
quite common in Japan.
<p>
The current patterns provided are:
<p>
<pre class=scheme>
<span class=variable>newline</span> <span class=comment>; general newline pattern (crlf, cr, lf)</span>
<span class=variable>integer</span> <span class=comment>; an integer</span>
<span class=variable>real</span> <span class=comment>; a real number (including scientific)</span>
<span class=variable>string</span> <span class=comment>; a "quoted" string</span>
<span class=variable>symbol</span> <span class=comment>; an R5RS Scheme symbol</span>
<span class=variable>ipv4-address</span> <span class=comment>; a numeric decimal ipv4 address</span>
<span class=variable>ipv6-address</span> <span class=comment>; a numeric hexadecimal ipv6 address</span>
<span class=variable>domain</span> <span class=comment>; a domain name</span>
<span class=variable>email</span> <span class=comment>; an email address</span>
<span class=variable>http-url</span> <span class=comment>; a URL beginning with https?://</span>
</pre>
<p>
Because of these issues the exact definitions of these patterns are
subject to be changed, but will be documented clearly when they are
finalized. More common patterns are also planned, but as what you
want increases in complexity it's probably better to use a real
parser.
<p>
<a name="SECTION_3.3"><h2>3.3 Supported PCRE Syntax</h2>
Since the PCRE syntax is so overwhelming complex, it's easier to just
list what we *don't* support for now. Refer to the
<a href="http://pcre.org/pcre.txt">PCRE documentation</a> for details. You
should be using the SRE syntax anyway!
<p>
Unicode character classes (\P) are not supported, but will be
in an upcoming release. \C named characters are not supported.
<p>
Callbacks, subroutine patterns and recursive patterns are not
supported. (*FOO) patterns are not supported and may never be.
<p>
\G and \K are not supported.
<p>
Octal character escapes are not supported because they are ambiguous
with back-references - just use hex character escapes.
<p>
Other than that everything should work, including named submatches,
zero-width assertions, conditional patterns, etc.
<p>
In addition, \< and \> act as beginning-of-word and end-of-word marks,
respectively, as in Emacs regular expressions.
<p>
Also, two escapes are provided to embed SRE patterns inside PCRE
strings, "\'<sre>" and "(*'<sre>)". For example, to match a
comma-delimited list of integers you could use
<p>
"\\'integer(,\\'integer)*"
<p>
and to match a URL in angle brackets you could use
<p>
"<('*http-url)>"
<p>
Note in the second example the enclosing "('*...)" syntax is needed
because the Scheme reader would consider the closing ">" as part of
the SRE symbol.
<p>
The following chart gives a quick reference from PCRE form to the SRE
equivalent:
<p>
<pre class=scheme>
<span class=comment>;; basic syntax</span>
<span class=string>"^"</span> <span class=comment>;; bos (or eos inside (?m: ...))</span>
<span class=string>"$"</span> <span class=comment>;; eos (or eos inside (?m: ...))</span>
<span class=string>"."</span> <span class=comment>;; nonl</span>
<span class=string>"a?"</span> <span class=comment>;; (? a)</span>
<span class=string>"a*"</span> <span class=comment>;; (* a)</span>
<span class=string>"a+"</span> <span class=comment>;; (+ a)</span>
<span class=string>"a??"</span> <span class=comment>;; (?? a)</span>
<span class=string>"a*?"</span> <span class=comment>;; (*? a)</span>
<span class=string>"a+?"</span> <span class=comment>;; (+? a)</span>
<span class=string>"a{n,m}"</span> <span class=comment>;; (** n m a)</span>
<span class=comment>;; grouping</span>
<span class=string>"(...)"</span> <span class=comment>;; (submatch ...)</span>
<span class=string>"(?:...)"</span> <span class=comment>;; (: ...)</span>
<span class=string>"(?i:...)"</span> <span class=comment>;; (w/nocase ...)</span>
<span class=string>"(?-i:...)"</span> <span class=comment>;; (w/case ...)</span>
<span class=string>"(?<name>...)"</span> <span class=comment>;; (=> <name>...)</span>
<span class=comment>;; character classes</span>
<span class=string>"[aeiou]"</span> <span class=comment>;; ("aeiou")</span>
<span class=string>"[^aeiou]"</span> <span class=comment>;; (~ "aeiou")</span>
<span class=string>"[a-z]"</span> <span class=comment>;; (/ "az") or (/ "a" "z")</span>
<span class=string>"[[:alpha:]]"</span> <span class=comment>;; alpha</span>
<span class=comment>;; assertions</span>
<span class=string>"(?=...)"</span> <span class=comment>;; (look-ahead ...)</span>
<span class=string>"(?!...)"</span> <span class=comment>;; (neg-look-ahead ...)</span>
<span class=string>"(?<=...)"</span> <span class=comment>;; (look-behind ...)</span>
<span class=string>"(?<!...)"</span> <span class=comment>;; (neg-look-behind ...)</span>
<span class=string>"(?(test)pass|fail)"</span> <span class=comment>;; (if test pass fail)</span>
<span class=string>"(*COMMIT)"</span> <span class=comment>;; commit</span>
</pre>
<p>
<a name="SECTION_3.4"><h2>3.4 Chunked String Matching</h2>
It's often desirable to perform regular expression matching over
sequences of characters not represented as a single string. The most
obvious example is a text-buffer data structure, but you may also want
to match over lists or trees of strings (i.e. ropes), over only
certain ranges within a string, over an input port, etc. With
existing regular expression libraries, the only way to accomplish this
is by converting the abstract sequence into a freshly allocated
string. This can be expensive, or even impossible if the object is a
text-buffer opened onto a 500MB file.
<p>
IrRegex provides a chunked string API specifically for this purpose.
You define a chunking API with
<p>
<h3>(make-irregex-chunker <get-next> <get-string> [<get-start> <get-end> <get-substring> <get-subchunk>])</h3>
where
<p>
<code class=scheme>(<span class=variable><get-next></span> <span class=variable>chunk</span>) <span class=keyword>=></span> </code> returns the next chunk, or <code class=scheme><span class=boolean>#f</span></code> if there are no more chunks
<p>
<code class=scheme>(<span class=variable><get-string></span> <span class=variable>chunk</span>) <span class=keyword>=></span> </code> a string source for the chunk
<p>
<code class=scheme>(<span class=variable><get-start></span> <span class=variable>chunk</span>) <span class=keyword>=></span> </code> the start index of the result of <code class=scheme><span class=variable><get-string></span></code> (defaults to always 0)
<p>
<code class=scheme>(<span class=variable><get-end></span> <span class=variable>chunk</span>) <span class=keyword>=></span> </code> the end (exclusive) of the string (defaults to <code class=scheme><span class=variable>string-length</span></code> of the source string)
<p>
<code class=scheme>(<span class=variable><get-substring></span> <span class=variable>cnk1</span> <span class=variable>i</span> <span class=variable>cnk2</span> <span class=variable>j</span>) <span class=keyword>=></span> </code> a substring for the range between the chunk <code class=scheme><span class=variable>cnk1</span></code> starting at index <code class=scheme><span class=variable>i</span></code> and ending at <code class=scheme><span class=variable>cnk2</span></code> at index <code class=scheme><span class=variable>j</span></code>
<p>
<code class=scheme>(<span class=variable><get-subchunk></span> <span class=variable>cnk1</span> <span class=variable>i</span> <span class=variable>cnk2</span> <span class=variable>j</span>) <span class=keyword>=></span> </code> as above but returns a new chunked data type instead of a string (optional)
<p>
There are two important constraints on the <code class=scheme><span class=variable><get-next></span></code> procedure.
It must return an <code class=scheme><span class=variable>eq?</span></code> identical object when called multiple times
on the same chunk, and it must not return a chunk with an empty string
(start == end). This second constraint is for performance reasons -
we push the work of possibly filtering empty chunks to the chunker
since there are many chunk types for which empty strings aren't
possible, and this work is thus not needed. Note that the initial
chunk passed to match on is allowed to be empty.
<p>
<code class=scheme><span class=variable><get-substring></span></code> is provided for possible performance improvements
- without it a default is used. <code class=scheme><span class=variable><get-subchunk></span></code> is optional -
without it you may not use <code class=scheme><span class=variable>irregex-match-subchunk</span></code> described above.
<p>
You can then match chunks of these types with the following
procedures:
<p>
<h3>(irregex-search/chunked <irx> <chunker> <chunk> [<start>])</h3>
<h3>(irregex-match/chunked <irx> <chunker> <chunk> [<start>])</h3>
These return normal match-data objects.
<p>