Debian Codegraph @

vendor/re/src/posix.mli

raw
1 (*
2 RE - A regular expression library
3
4 Copyright (C) 2001 Jerome Vouillon
5 email: [email protected]
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation, with
10 linking exception; either version 2.1 of the License, or (at
11 your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *)
22
23 (** References:
24 - {{:http://www.opengroup.org/onlinepubs/007908799/xbd/re.html} re}
25 - {{:http://www.opengroup.org/onlinepubs/007908799/xsh/regcomp.html} regcomp}
26
27 Example of how to use this module (to parse some IRC logs):
28
29 {[
30 type msg =
31 { time : string
32 ; author : string
33 ; content : string
34 }
35
36 let re = Core.compile (Re_posix.re "([^:].*:[^:]*:[^:]{2})<.([^>]+)> (.+)$")
37
38 (* parse a line *)
39 let match_line line =
40 try
41 let substrings = Core.exec re line in
42 let groups = Core.get_all substrings in
43 (* groups can be obtained directly by index within [substrings] *)
44 Some { time = groups.(1); author = groups.(2); content = groups.(3) }
45 with
46 | Not_found -> None (* regex didn't match *)
47 ;;
48 ]} *)
49
50 (* XXX Character classes *)
51
52 exception Parse_error
53
54 (** Errors that can be raised during the parsing of the regular expression *)
55 exception Not_supported
56
57 type opt =
58 [ `ICase
59 | `NoSub
60 | `Newline
61 ]
62
63 (** Parsing of a Posix extended regular expression *)
64 val re : ?opts:opt list -> string -> Core.t
65
66 (** [compile r] is defined as [Core.compile (Core.longest r)] *)
67 val compile : Core.t -> Core.re
68
69 (** [compile_pat ?opts regex] compiles the Posix extended regular expression [regexp] *)
70 val compile_pat : ?opts:opt list -> string -> Core.re
71
72 (*
73 Deviation from the standard / ambiguities in the standard
74 ---------------------------------------------------------
75 We tested the behavior of the Linux library (glibc) and the Solaris
76 library.
77
78 (1) An expression [efg] should be parsed as [(ef)g].
79 All implementations parse it as [e(fg)].
80 (2) When matching the pattern "((a)|b)*" against the string "ab",
81 the sub-expression "((a)|b)" should match "b", and the
82 sub-expression "(a)" should not match anything.
83 In both implementation, the sub-expression "(a)" matches "a".
84 (3) When matching the pattern "(aa?)*" against the string "aaa", it is
85 not clear whether the final match of the sub-expression "(aa?)" is
86 the last "a" (all matches of the sub-expression are successively
87 maximized), or "aa" (the final match is maximized).
88 Both implementations implements the first case.
89 (4) When matching the pattern "((a?)|b)*" against the string "ab",
90 the sub-expression "((a?)|b)" should match the empty string at the
91 end of the string (it is better to match the empty string than to
92 match nothing).
93 In both implementations, this sub-expression matches "b".
94 (Strangely, in the Linux implementation, the sub-expression "(a?)"
95 correctly matches the empty string at the end of the string)
96
97 This library behaves the same way as the other libraries for all
98 points, except for (2) and (4) where it follows the standard.
99
100 The behavior of this library in theses four cases may change in future
101 releases.
102 *)