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
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
<title>Lrexlib 2.1 Reference Manual</title>
<link rel="stylesheet" href="lrexlib.css" type="text/css" />
</head>
<body>
<div class="document" id="lrexlib-2-1-reference-manual">
<h1 class="title">Lrexlib 2.1 Reference Manual</h1>
<div class="contents topic">
<p class="topic-title first"><a id="table-of-contents" name="table-of-contents">Table of Contents</a></p>
<ul class="simple">
<li><a class="reference" href="#introduction" id="id1" name="id1">Introduction</a></li>
<li><a class="reference" href="#notes" id="id2" name="id2">Notes</a></li>
<li><a class="reference" href="#lrexlib-functions" id="id3" name="id3">Lrexlib Functions</a><ul>
<li><a class="reference" href="#match" id="id4" name="id4">match</a></li>
<li><a class="reference" href="#find" id="id5" name="id5">find</a></li>
<li><a class="reference" href="#gmatch" id="id6" name="id6">gmatch</a></li>
<li><a class="reference" href="#gsub" id="id7" name="id7">gsub</a></li>
<li><a class="reference" href="#split" id="id8" name="id8">split</a></li>
<li><a class="reference" href="#plainfind" id="id9" name="id9">plainfind</a></li>
<li><a class="reference" href="#new" id="id10" name="id10">new</a></li>
<li><a class="reference" href="#flags" id="id11" name="id11">flags</a></li>
<li><a class="reference" href="#config" id="id12" name="id12">config</a></li>
<li><a class="reference" href="#version" id="id13" name="id13">version</a></li>
</ul>
</li>
<li><a class="reference" href="#lrexlib-methods" id="id14" name="id14">Lrexlib Methods</a><ul>
<li><a class="reference" href="#tfind" id="id15" name="id15">tfind</a></li>
<li><a class="reference" href="#exec" id="id16" name="id16">exec</a></li>
<li><a class="reference" href="#dfa-exec" id="id17" name="id17">dfa_exec</a></li>
</ul>
</li>
<li><a class="reference" href="#incompatibilities-with-the-previous-versions" id="id18" name="id18">Incompatibilities with the Previous Versions</a></li>
</ul>
</div>
<hr class="docutils" />
<div class="section">
<h2><a class="toc-backref" href="#id1" id="introduction" name="introduction">Introduction</a></h2>
<p><strong>Lrexlib</strong> provides bindings of the two principal regular expression library
interfaces (<a class="reference" href="http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html">POSIX</a> and <a class="reference" href="http://www.pcre.org/pcre.txt">PCRE</a>) to <a class="reference" href="http://www.lua.org">Lua</a> 5.1.</p>
<p><strong>Lrexlib</strong> builds into shared libraries called by default <em>rex_posix.so</em> and
<em>rex_pcre.so</em>, which can be used with <em>require</em>.</p>
<p><strong>Lrexlib</strong> is copyright Reuben Thomas 2000-2007 and copyright Shmuel Zeigerman
2004-2007, and is released under the MIT license.</p>
</div>
<hr class="docutils" />
<div class="section">
<h2><a class="toc-backref" href="#id2" id="notes" name="notes">Notes</a></h2>
<ol class="arabic">
<li><p class="first">Most functions and methods in Lrexlib have mandatory and optional arguments.
There are no dependencies between arguments in Lrexlib's functions and
methods. Any optional argument can be supplied as <tt class="docutils literal"><span class="pre">nil</span></tt> (or omitted if it
is trailing one), the library will then use the default value for that
argument.</p>
</li>
<li><p class="first">This document uses the following syntax for optional arguments: they are
bracketed separately, and commas are left outside brackets, e.g.:</p>
<pre class="literal-block">
MyFunc (arg1, arg2, [arg3], [arg4])
</pre>
</li>
<li><p class="first">Throughout this document, the identifier <em>rex</em> is used in place of either
<em>rex_posix</em> or <em>rex_pcre</em>, that are the default namespaces for the
corresponding libraries.</p>
</li>
<li><p class="first">All functions receiving a regular expression pattern as an argument will
generate an error if that pattern is found invalid by the used <a class="reference" href="http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html">POSIX</a> / <a class="reference" href="http://www.pcre.org/pcre.txt">PCRE</a>
library.</p>
</li>
</ol>
<ol class="arabic" id="cf" start="5">
<li><p class="first">The default value for <em>compilation flags</em> (<em>cf</em>) that Lrexlib uses when
the parameter is not supplied or <tt class="docutils literal"><span class="pre">nil</span></tt>, is:</p>
<blockquote>
<ul class="simple">
<li>0 for PCRE</li>
<li>REG_EXTENDED for POSIX regex library</li>
</ul>
</blockquote>
</li>
</ol>
<ol class="arabic" id="ef" start="6">
<li><p class="first">The default value for <em>execution flags</em> (<em>ef</em>) that Lrexlib uses when
the parameter is not supplied or <tt class="docutils literal"><span class="pre">nil</span></tt>, is:</p>
<blockquote>
<ul class="simple">
<li>0 for PCRE</li>
<li>0 for standard POSIX regex library</li>
<li>REG_STARTEND for those POSIX regex libraries that support it,
e.g. Spencer's.</li>
</ul>
</blockquote>
</li>
</ol>
</div>
<hr class="docutils" />
<div class="section">
<h2><a class="toc-backref" href="#id3" id="lrexlib-functions" name="lrexlib-functions">Lrexlib Functions</a></h2>
<div class="section">
<h3><a class="toc-backref" href="#id4" id="match" name="match">match</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">rex.match</span> <span class="pre">(subj,</span> <span class="pre">patt,</span> <span class="pre">[init],</span> <span class="pre">[cf],</span> <span class="pre">[ef],</span> <span class="pre">[lo])</span></tt></p>
<p>The function searches for the first match of the regexp <em>patt</em> in the string
<em>subj</em>, starting from offset <em>init</em>, subject to flags <em>cf</em> and <em>ef</em>.</p>
<p>PCRE: A locale <em>lo</em> may be specified.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="51%" />
<col width="13%" />
<col width="21%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>patt</td>
<td>regular expression pattern</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[init]</td>
<td>start offset in the subject
(can be negative)</td>
<td>number</td>
<td>1</td>
</tr>
<tr><td>[cf]</td>
<td>compilation flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#cf">cf</a></td>
</tr>
<tr><td>[ef]</td>
<td>execution flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#ef">ef</a></td>
</tr>
<tr><td>[lo]</td>
<td>[PCRE] locale</td>
<td>string</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns on success:</strong></dt>
<dd><ol class="first last arabic simple">
<li>All substring matches ("captures"), in the order they appear in the
pattern. <tt class="docutils literal"><span class="pre">false</span></tt> is returned for sub-patterns that did not participate in
the match. If the pattern specified no captures then the whole matched
substring is returned.</li>
</ol>
</dd>
<dt><strong>Returns on failure:</strong></dt>
<dd><ol class="first last arabic simple">
<li><tt class="docutils literal"><span class="pre">nil</span></tt></li>
</ol>
</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id5" id="find" name="find">find</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">rex.find</span> <span class="pre">(subj,</span> <span class="pre">patt,</span> <span class="pre">[init],</span> <span class="pre">[cf],</span> <span class="pre">[ef],</span> <span class="pre">[lo])</span></tt></p>
<p>The function searches for the first match of the regexp <em>patt</em> in the string
<em>subj</em>, starting from offset <em>init</em>, subject to flags <em>cf</em> and <em>ef</em>.</p>
<p>PCRE: A locale <em>lo</em> may be specified.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="51%" />
<col width="13%" />
<col width="21%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>patt</td>
<td>regular expression pattern</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[init]</td>
<td>start offset in the subject
(can be negative)</td>
<td>number</td>
<td>1</td>
</tr>
<tr><td>[cf]</td>
<td>compilation flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#cf">cf</a></td>
</tr>
<tr><td>[ef]</td>
<td>execution flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#ef">ef</a></td>
</tr>
<tr><td>[lo]</td>
<td>[PCRE] locale</td>
<td>string</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns on success:</strong></dt>
<dd><ol class="first last arabic simple">
<li>The start point of the match (a number).</li>
<li>The end point of the match (a number).</li>
<li>All substring matches ("captures"), in the order they appear in the
pattern. <tt class="docutils literal"><span class="pre">false</span></tt> is returned for sub-patterns that did not participate in
the match.</li>
</ol>
</dd>
<dt><strong>Returns on failure:</strong></dt>
<dd><ol class="first last arabic simple">
<li><tt class="docutils literal"><span class="pre">nil</span></tt></li>
</ol>
</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id6" id="gmatch" name="gmatch">gmatch</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">rex.gmatch</span> <span class="pre">(subj,</span> <span class="pre">patt,</span> <span class="pre">[cf],</span> <span class="pre">[ef],</span> <span class="pre">[lo])</span></tt></p>
<p>The function is intended for use in the <em>generic for</em> Lua construct.
It returns an iterator for repeated matching of the pattern <em>patt</em> in
the string <em>subj</em>, subject to flags <em>cf</em> and <em>ef</em>.</p>
<p>PCRE: A locale <em>lo</em> may be specified.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="53%" />
<col width="10%" />
<col width="22%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>patt</td>
<td>regular expression pattern</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[cf]</td>
<td>compilation flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#cf">cf</a></td>
</tr>
<tr><td>[ef]</td>
<td>execution flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#ef">ef</a></td>
</tr>
<tr><td>[lo]</td>
<td>[PCRE] locale</td>
<td>string</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<p>The iterator function is called by Lua. On every iteration (that is, on every
match), it returns all captures in the order they appear in the pattern (or the
entire match if the pattern specified no captures). The iteration will continue
till the subject fails to match.</p>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id7" id="gsub" name="gsub">gsub</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">rex.gsub</span> <span class="pre">(subj,</span> <span class="pre">patt,</span> <span class="pre">repl,</span> <span class="pre">[n],</span> <span class="pre">[cf],</span> <span class="pre">[ef],</span> <span class="pre">[lo])</span></tt></p>
<p>The function searches for all matches of the pattern <em>patt</em> in the string <em>subj</em>
and substitutes the found matches according to the parameter <em>repl</em> (see details
below).</p>
<p>PCRE: A locale <em>lo</em> may be specified.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="11%" />
<col width="43%" />
<col width="30%" />
<col width="16%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>patt</td>
<td>regular expression pattern</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>repl</td>
<td>substitution source</td>
<td>string, function or table</td>
<td>n/a</td>
</tr>
<tr><td>[n]</td>
<td>maximum number of matches to search
for; unlimited if not supplied</td>
<td>number</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
<tr><td>[cf]</td>
<td>compilation flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#cf">cf</a></td>
</tr>
<tr><td>[ef]</td>
<td>execution flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#ef">ef</a></td>
</tr>
<tr><td>[lo]</td>
<td>[PCRE] locale</td>
<td>string</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns:</strong></dt>
<dd><ol class="first last arabic simple">
<li>The subject string with the substitutions made.</li>
<li>Number of matches found.</li>
</ol>
</dd>
<dt><strong>Details:</strong></dt>
<dd><p class="first">The parameter <em>repl</em> can be either a string, a function or a table. The
function behaves differently depending on the <em>repl</em> type:</p>
<ol class="arabic simple">
<li>If <em>repl</em> is a <em>string</em> then it is treated as a template for substitution,
where the %X occurences in <em>repl</em> are handled in a special way, depending
on the value of the character X:</li>
</ol>
<blockquote>
<ul class="simple">
<li>if X represents a digit, then each %X occurence is substituted by the
value of the X-th submatch (capture), with the following cases handled
specially:<ul>
<li>each %0 is substituted by the entire match</li>
<li>if the pattern contains no captures, then each %1 is substituted by the
entire match</li>
<li>any other %X where X is greater than the number of captures in the
pattern will generate an error ("invalid capture index")</li>
<li>if the pattern does contain a capture with number X but that capture
didn't participate in the match, then %X is substituted by an empty
string</li>
</ul>
</li>
<li>if X is any non-digit character then %X is substituted by X</li>
<li>all parts of <em>repl</em> other than %X are copied to the output string
verbatim.</li>
</ul>
</blockquote>
<ol class="arabic simple" start="2">
<li>If <em>repl</em> is a <em>function</em> then it gets called on each match with the
submatches passed as parameters (if there are no submatches then the entire
match is passed as the only parameter). The substitution string is derived
depending on the first return value of function <em>repl</em>:</li>
</ol>
<blockquote>
<ul class="simple">
<li>if it is a string then it is used as a substitution for the current match.</li>
<li>if it is either of nothing, <tt class="docutils literal"><span class="pre">nil</span></tt> or <tt class="docutils literal"><span class="pre">false</span></tt> then no substitution is
made.</li>
<li>values of other types generate an error.</li>
</ul>
<p>Though <a class="reference" href="#gsub">gsub</a> is in general consistent with the API and behavior of Lua's
<em>string.gsub</em>, it has one extension with regards to <em>string.gsub</em> behavior:</p>
<ul class="simple">
<li>if function <em>repl</em> returns more than one value and its second return value
is the literal string <em>"break"</em>, then <a class="reference" href="#gsub">gsub</a> stops searching for further
matches in the subject and returns.</li>
</ul>
</blockquote>
<ol class="arabic simple" start="3">
<li>If <em>repl</em> is a <em>table</em> then the first submatch (or the entire match if
there are no submatches) is used as the key and the value stored in <em>repl</em>
under that key is used for substitution depending on its type.</li>
</ol>
<blockquote class="last">
<ul class="simple">
<li>If no value is stored under the key but <em>repl</em> has a metatable with the
<em>__index</em> field set then the correspondent metamethod will be called for
obtaining the value.</li>
<li>The obtained value is used for the substitution following exactly same
rules as for the first return value of <em>repl</em> described in the above
paragraph.</li>
</ul>
</blockquote>
</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id8" id="split" name="split">split</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">rex.split</span> <span class="pre">(subj,</span> <span class="pre">sep,</span> <span class="pre">[cf],</span> <span class="pre">[ef],</span> <span class="pre">[lo])</span></tt></p>
<p>The function is intended for use in the <em>generic for</em> Lua construct.
It is used for splitting a subject string <em>subj</em> into parts (<em>sections</em>).
The <em>sep</em> parameter is a regular expression pattern representing
<strong>separators</strong> between the sections.</p>
<p>The function returns an iterator for repeated matching of the pattern <em>sep</em> in
the string <em>subj</em>, subject to flags <em>cf</em> and <em>ef</em>.</p>
<p>PCRE: A locale <em>lo</em> may be specified.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="53%" />
<col width="10%" />
<col width="22%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>sep</td>
<td>separator (regular expression
pattern)</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[cf]</td>
<td>compilation flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#cf">cf</a></td>
</tr>
<tr><td>[ef]</td>
<td>execution flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#ef">ef</a></td>
</tr>
<tr><td>[lo]</td>
<td>[PCRE] locale</td>
<td>string</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<p><strong>On every iteration pass, the iterator returns:</strong></p>
<blockquote>
<ol class="arabic simple">
<li>A subject section (can be an empty string), followed by</li>
<li>All captures in the order they appear in the <em>sep</em> pattern (or the entire
match if the <em>sep</em> pattern specified no captures). If there is no match
(this can occur only in the last iteration), then nothing is returned after
the subject section.</li>
</ol>
</blockquote>
<p>The iteration will continue till the end of the subject. Unlike <a class="reference" href="#gmatch">gmatch</a>, there
will always be at least one iteration pass, even if there's no matches in the
subject.</p>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id9" id="plainfind" name="plainfind">plainfind</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">rex.plainfind</span> <span class="pre">(subj,</span> <span class="pre">patt,</span> <span class="pre">[init],</span> <span class="pre">[ci])</span></tt></p>
<p>The function searches for the first match of the string <em>patt</em> in the subject
<em>subj</em>, starting from offset <em>init</em>.</p>
<blockquote>
<ul class="simple">
<li>The string <em>patt</em> is not regular expression, all its characters stand for
themselves.</li>
<li>Both strings <em>subj</em> and <em>patt</em> can have embedded zeros.</li>
<li>The flag <em>ci</em> specifies case-insensitive search (current locale is used).</li>
<li>This function uses neither PCRE nor POSIX regex library.</li>
</ul>
<table border="1" class="docutils">
<colgroup>
<col width="16%" />
<col width="47%" />
<col width="14%" />
<col width="23%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>patt</td>
<td>text to find</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[init]</td>
<td>start offset in the subject
(can be negative)</td>
<td>number</td>
<td>1</td>
</tr>
<tr><td>[ci]</td>
<td>case insensitive search</td>
<td>boolean</td>
<td><tt class="docutils literal"><span class="pre">false</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns on success:</strong></dt>
<dd><ol class="first last arabic simple">
<li>The start point of the match (a number).</li>
<li>The end point of the match (a number).</li>
</ol>
</dd>
<dt><strong>Returns on failure:</strong></dt>
<dd><ol class="first last arabic simple">
<li><tt class="docutils literal"><span class="pre">nil</span></tt></li>
</ol>
</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id10" id="new" name="new">new</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">rex.new</span> <span class="pre">(patt,</span> <span class="pre">[cf],</span> <span class="pre">[lo])</span></tt></p>
<p>The functions compiles regular expression <em>patt</em> into a regular expression
object whose internal representation is correspondent to the library used (PCRE
or POSIX regex). The returned result then can be used by the methods <a class="reference" href="#tfind">tfind</a>,
<a class="reference" href="#exec">exec</a> and <a class="reference" href="#dfa-exec">dfa_exec</a>. Regular expression objects are automatically garbage
collected.</p>
<p>PCRE: A locale <em>lo</em> may be specified.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="51%" />
<col width="13%" />
<col width="21%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>patt</td>
<td>regular expression pattern</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[cf]</td>
<td>compilation flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#cf">cf</a></td>
</tr>
<tr><td>[lo]</td>
<td>[PCRE] locale</td>
<td>string</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns:</strong></dt>
<dd><ol class="first last arabic simple">
<li>Compiled regular expression (a userdata).</li>
</ol>
</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id11" id="flags" name="flags">flags</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">rex.flags</span> <span class="pre">([tb])</span></tt></p>
<p>This function returns a table containing numeric values of the constants defined
by the used regex library (either PCRE or POSIX). Those constants are keyed by
their names (strings). If the table argument <em>tb</em> is supplied then it is used as
the output table, else a new table is created.</p>
<p>The constants contained in the returned table can then be used in most functions
and methods where <em>compilation flags</em> or <em>execution flags</em> can be specified.
They can also be used for comparing with return codes of some functions and
methods for determining the reason of failure. For details, see <a class="reference" href="http://www.pcre.org/pcre.txt">PCRE</a> and <a class="reference" href="http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html">POSIX</a>
documentation.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="52%" />
<col width="13%" />
<col width="21%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>[tb]</td>
<td>a table for placing results into</td>
<td>table</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns:</strong></dt>
<dd><ol class="first last arabic simple">
<li>A table filled with the results.</li>
</ol>
</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id12" id="config" name="config">config</a></h3>
<p>[PCRE only. See <em>pcre_config</em> in the <a class="reference" href="http://www.pcre.org/pcre.txt">PCRE</a> docs.]</p>
<p><tt class="funcdef docutils literal"><span class="pre">rex.config</span> <span class="pre">([tb])</span></tt></p>
<p>This function returns a table containing the values of the configuration
parameters used at PCRE library build-time. Those parameters (numbers) are
keyed by their names (strings). If the table argument <em>tb</em> is supplied then it
is used as the output table, else a new table is created.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="52%" />
<col width="13%" />
<col width="21%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>[tb]</td>
<td>a table for placing results into</td>
<td>table</td>
<td><tt class="docutils literal"><span class="pre">nil</span></tt></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns:</strong></dt>
<dd><ol class="first last arabic simple">
<li>A table filled with the results.</li>
</ol>
</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id13" id="version" name="version">version</a></h3>
<p>[PCRE only. See <em>pcre_version</em> in the <a class="reference" href="http://www.pcre.org/pcre.txt">PCRE</a> docs.]</p>
<p><tt class="funcdef docutils literal"><span class="pre">rex.version</span> <span class="pre">()</span></tt></p>
<p>This function returns a string containing the version of the used PCRE library
and its release date.</p>
</div>
</div>
<hr class="docutils" />
<div class="section">
<h2><a class="toc-backref" href="#id14" id="lrexlib-methods" name="lrexlib-methods">Lrexlib Methods</a></h2>
<div class="section">
<h3><a class="toc-backref" href="#id15" id="tfind" name="tfind">tfind</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">r:tfind</span> <span class="pre">(subj,</span> <span class="pre">[init],</span> <span class="pre">[ef])</span></tt></p>
<p>The method searches for the first match of the compiled regexp <em>r</em> in the
string <em>subj</em>, starting from offset <em>init</em>, subject to execution flags <em>ef</em>.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="14%" />
<col width="54%" />
<col width="12%" />
<col width="20%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>r</td>
<td>regex object produced by <a class="reference" href="#new">new</a></td>
<td>userdata</td>
<td>n/a</td>
</tr>
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[init]</td>
<td>start offset in the subject
(can be negative)</td>
<td>number</td>
<td>1</td>
</tr>
<tr><td>[ef]</td>
<td>execution flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#ef">ef</a></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns on success:</strong></dt>
<dd><ol class="first last arabic simple">
<li>The start point of the match (a number).</li>
<li>The end point of the match (a number).</li>
<li>Substring matches ("captures" in Lua terminology) are returned as a third
result, in a table. This table contains <tt class="docutils literal"><span class="pre">false</span></tt> in the positions where the
corresponding sub-pattern did not participate in the match.<ol class="arabic">
<li>PCRE: if <em>named subpatterns</em> are used then the table also contains
substring matches keyed by their correspondent subpattern names
(strings).</li>
</ol>
</li>
</ol>
</dd>
<dt><strong>Returns on failure:</strong></dt>
<dd><ol class="first last arabic simple">
<li><tt class="docutils literal"><span class="pre">nil</span></tt></li>
</ol>
</dd>
<dt><strong>Notes:</strong></dt>
<dd><ol class="first last arabic simple">
<li>If <em>named subpatterns</em> (see <a class="reference" href="http://www.pcre.org/pcre.txt">PCRE</a> docs) are used then the returned table
also contains substring matches keyed by their correspondent subpattern
names (strings).</li>
</ol>
</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id16" id="exec" name="exec">exec</a></h3>
<p><tt class="funcdef docutils literal"><span class="pre">r:exec</span> <span class="pre">(subj,</span> <span class="pre">[init],</span> <span class="pre">[ef])</span></tt></p>
<p>The method searches for the first match of the compiled regexp <em>r</em> in the
string <em>subj</em>, starting from offset <em>init</em>, subject to execution flags <em>ef</em>.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="14%" />
<col width="54%" />
<col width="12%" />
<col width="20%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>r</td>
<td>regex object produced by <a class="reference" href="#new">new</a></td>
<td>userdata</td>
<td>n/a</td>
</tr>
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[init]</td>
<td>start offset in the subject
(can be negative)</td>
<td>number</td>
<td>1</td>
</tr>
<tr><td>[ef]</td>
<td>execution flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#ef">ef</a></td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns on success:</strong></dt>
<dd><ol class="first last arabic simple">
<li>The start point of the first match (a number).</li>
<li>The end point of the first match (a number).</li>
<li>The offsets of substring matches ("captures" in Lua terminology) are
returned as a third result, in a table. This table contains <tt class="docutils literal"><span class="pre">false</span></tt> in the
positions where the corresponding sub-pattern did not participate in the
match.<ol class="arabic">
<li>PCRE: if <em>named subpatterns</em> are used then the table also contains
substring matches keyed by their correspondent subpattern names
(strings).</li>
</ol>
</li>
</ol>
</dd>
<dt><strong>Returns on failure:</strong></dt>
<dd><ol class="first last arabic simple">
<li><tt class="docutils literal"><span class="pre">nil</span></tt></li>
</ol>
</dd>
<dt><strong>Example:</strong></dt>
<dd>If the whole match is at offsets 10,20 and substring matches are at offsets
12,14 and 16,19 then the function returns the following: 10, 20,
{ 12,14,16,19 }.</dd>
</dl>
</div>
<hr class="docutils" />
<div class="section">
<h3><a class="toc-backref" href="#id17" id="dfa-exec" name="dfa-exec">dfa_exec</a></h3>
<p>[PCRE 6.0 and later. See <em>pcre_dfa_exec</em> in the <a class="reference" href="http://www.pcre.org/pcre.txt">PCRE</a> docs.]</p>
<p><tt class="funcdef docutils literal"><span class="pre">r:dfa_exec</span> <span class="pre">(subj,</span> <span class="pre">[init],</span> <span class="pre">[ef],</span> <span class="pre">[ovecsize],</span> <span class="pre">[wscount])</span></tt></p>
<p>The method matches a compiled regular expression <em>r</em> against a given subject
string <em>subj</em>, using a DFA matching algorithm.</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="15%" />
<col width="54%" />
<col width="12%" />
<col width="19%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Type</th>
<th class="head">Default Value</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>r</td>
<td>regex object produced by <a class="reference" href="#new">new</a></td>
<td>userdata</td>
<td>n/a</td>
</tr>
<tr><td>subj</td>
<td>subject</td>
<td>string</td>
<td>n/a</td>
</tr>
<tr><td>[init]</td>
<td>start offset in the subject
(can be negative)</td>
<td>number</td>
<td>1</td>
</tr>
<tr><td>[ef]</td>
<td>execution flags (bitwise OR)</td>
<td>number</td>
<td><a class="reference" href="#ef">ef</a></td>
</tr>
<tr><td>[ovecsize]</td>
<td>size of the array for result offsets</td>
<td>number</td>
<td>100</td>
</tr>
<tr><td>[wscount]</td>
<td>number of elements in the working
space array</td>
<td>number</td>
<td>50</td>
</tr>
</tbody>
</table>
</blockquote>
<dl class="docutils">
<dt><strong>Returns on success (either full or partial match):</strong></dt>
<dd><ol class="first last arabic simple">
<li>The start point of the matches found (a number).</li>
<li>A table containing the end points of the matches found, the longer matches
first.</li>
<li>The return value of the underlying <em>pcre_dfa_exec</em> call (a number).</li>
</ol>
</dd>
<dt><strong>Returns on failure (no match):</strong></dt>
<dd><ol class="first last arabic simple">
<li><tt class="docutils literal"><span class="pre">nil</span></tt></li>
</ol>
</dd>
<dt><strong>Example:</strong></dt>
<dd>If there are 3 matches found starting at offset 10 and ending at offsets 15, 20
and 25 then the function returns the following: 10, { 25,20,15 }, 3.</dd>
</dl>
</div>
</div>
<hr class="docutils" />
<div class="section">
<h2><a class="toc-backref" href="#id18" id="incompatibilities-with-the-previous-versions" name="incompatibilities-with-the-previous-versions">Incompatibilities with the Previous Versions</a></h2>
<p><strong>The following changes are incompatible with Lrexlib version 1.19:</strong></p>
<blockquote>
<ol class="arabic simple">
<li>Lua 5.1 is required</li>
<li>Functions <tt class="docutils literal"><span class="pre">newPCRE</span></tt> and <tt class="docutils literal"><span class="pre">newPOSIX</span></tt> renamed to <a class="reference" href="#new">new</a></li>
<li>Functions <tt class="docutils literal"><span class="pre">flagsPCRE</span></tt> and <tt class="docutils literal"><span class="pre">flagsPOSIX</span></tt> renamed to <a class="reference" href="#flags">flags</a></li>
<li>Function <tt class="docutils literal"><span class="pre">versionPCRE</span></tt> renamed to <a class="reference" href="#version">version</a></li>
<li>Method <tt class="docutils literal"><span class="pre">match</span></tt> renamed to <a class="reference" href="#tfind">tfind</a></li>
<li>Method <tt class="docutils literal"><span class="pre">gmatch</span></tt> removed (similar functionality is provided by function
<a class="reference" href="#gmatch">gmatch</a>)</li>
<li>Method <a class="reference" href="#exec">exec</a>: the returned table may additionally contain <em>named
subpatterns</em> (PCRE only)</li>
</ol>
</blockquote>
<p><strong>The following changes are incompatible with Lrexlib version 2.0:</strong></p>
<blockquote>
<ol class="arabic simple">
<li><a class="reference" href="#match">match</a>, <a class="reference" href="#find">find</a>, <a class="reference" href="#tfind">tfind</a>, <a class="reference" href="#exec">exec</a>, <a class="reference" href="#dfa-exec">dfa_exec</a>: only one value (a <tt class="docutils literal"><span class="pre">nil</span></tt>) is
returned when the subject does not match the pattern. Any other failure
generates an error.</li>
</ol>
</blockquote>
</div>
</div>
<div class="footer">
<hr class="footer" />
Generated on: 2007-02-12 18:37 UTC.
</div>
</body>
</html>
|