1
(* $Id: netstring_pcre.mlip 1588 2011-04-28 13:59:54Z gerd $ *)
3
(** Wrapper for regexps with PCRE syntax
5
* This module is a version of [Str] with a thread-safe interface,
6
* implemented using Pcre.
8
* This modules processes PCRE-style regular expressions. If you like
9
* to write [Str]-like regexps, you can also use {!Netstring_str} with
10
* almost the same signature.
12
* {b The functions in this module are unavailable if Ocamlnet is built
13
* with [Str] as implementation of regular expressions! They will always
14
* raise [Invalid_argument]!}
17
(** Supported regexp syntax: See pcrepattern(3). *)
20
type regexp = Pcre.regexp
21
(** The type of regular expressions; now based on [Pcre] *)
28
type split_result = Pcre.split_result =
31
| Group of int * string
33
(** Here we keep compatiblity with [Pcre] *)
38
| Group of int * string
43
(** The type of matching results *)
45
val regexp: string -> regexp
46
(** Parses a regexp *)
47
val regexp_case_fold: string -> regexp
48
(** Parses a case-insensitive regexp *)
49
val quote: string -> string
50
(** Quotes a string such that it can be included in a regexp *)
51
val regexp_string: string -> regexp
52
(** Returns a regexp that matches exactly the string *)
53
val regexp_string_case_fold: string -> regexp
54
(** Returns a case-insensitive regexp that matches exactly the string *)
57
regexp -> string -> int -> result option
58
(** Matches the string at the position with the regexp. Returns
59
* [None] if no match is found. Returns [Some r] on success,
60
* and [r] describes the match.
64
regexp -> string -> int -> (int * result)
65
(** Searches a match of the string with the regexp, starting at
66
* the position and in forward direction.
67
* Raises [Not_found] if no match could be found.
68
* Returns [(p,r)] when a match at position [p] is found,
73
regexp -> string -> int -> (int * result)
74
(** Searches a match of the string with the regexp, starting at
75
* the position and in backward direction.
76
* Raises [Not_found] if no match could be found.
77
* Returns [(p,r)] when a match at position [p] is found,
82
string_partial_match: not available
85
(* The ~groups option is ignored *)
87
val matched_string : result -> string -> string
88
(** Extracts the matched part from the string. The string argument
89
* must be the same string passed to [string_match] or the search
90
* functions, and the result argument must be the corresponding
94
val match_beginning : result -> int
95
(** Returns the position where the matched part begins *)
97
val match_end : result -> int
98
(** Returns the position where the matched part ends *)
100
val matched_group : result -> int -> string -> string
101
(** Extracts the substring the nth group matches from the whole
102
* string. The string argument
103
* must be the same string passed to [string_match] or the search
104
* functions, and the result argument must be the corresponding
108
val group_beginning : result -> int -> int
109
(** Returns the position where the substring matching the nth
113
val group_end : result -> int -> int
114
(** Returns the position where the substring matching the nth
119
val global_replace: regexp -> string -> string -> string
120
(** [global_replace re templ s]: Replaces all matchings of [re] in
123
* In [templ] one can refer to matched groups by the backslash notation:
124
* [\1] refers to the first group, [\2] to the second etc.
125
* [\0] is the whole match. [\\ ] is the backslash character.
128
val replace_first: regexp -> string -> string -> string
129
(** [replace_first re templ s]: Replaces the first match of [re] in
132
* In [templ] one can refer to matched groups by the backslash notation:
133
* [\1] refers to the first group, [\2] to the second etc.
134
* [\0] is the whole match. [\\ ] is the backslash character.
137
val global_substitute:
138
regexp -> (result -> string -> string) -> string -> string
139
(** [global_substitute re subst s]: Applies the substitution function
140
* [subst] to all matchings of [re] in [s], and returns the
141
* transformed string. [subst] is called with the current [result]
142
* of the match and the whole string [s].
145
val substitute_first:
146
regexp -> (result -> string -> string) -> string -> string
147
(** [substitute_first re subst s]: Applies the substitution function
148
* [subst] to the first matching of [re] in [s], and returns the
149
* transformed string. [subst] is called with the current [result]
150
* of the match and the whole string [s].
154
(* replace_matched: not available *)
156
val split: regexp -> string -> string list
157
(** Splits the string according to the regexp in substrings.
158
* Occurrences of the delimiter at the beginning and the end
162
val bounded_split: regexp -> string -> int -> string list
163
(** Splits into at most [n] substrings, based on [split] *)
164
val split_delim: regexp -> string -> string list
165
(** Same as [split], but occurrences of the delimiter at the beginning
166
* and the end are returned as empty strings
168
val bounded_split_delim: regexp -> string -> int -> string list
169
(** Splits into at most [n] substrings, based on [split_delim] *)
170
val full_split: regexp -> string -> split_result list
171
(** Like [split_delim], but returns the delimiters in the result *)
172
val bounded_full_split: regexp -> string -> int -> split_result list
173
(** Splits into at most [n] substrings, based on [full_split] *)
175
val string_before: string -> int -> string
176
(** The first [n] characters of a string *)
177
val string_after: string -> int -> string
178
(** The last [n] characters of a string *)
179
val first_chars: string -> int -> string
180
(** Same as [string_before] *)
181
val last_chars: string -> int -> string
182
(** Same as [string_after] *)