| 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 |
(** Module [Re]: code for creating and using regular expressions, |
| 24 |
independently of regular expression syntax. *) |
| 25 |
|
| 26 |
(** Regular expression *) |
| 27 |
type t = Ast.t |
| 28 |
|
| 29 |
(** Compiled regular expression *) |
| 30 |
type re = Compile.re |
| 31 |
|
| 32 |
(** Manipulate matching groups. *) |
| 33 |
module Group : sig |
| 34 |
(** Information about groups in a match. As is conventional, every |
| 35 |
match implicitly has a group 0 that covers the whole match, and |
| 36 |
explicit groups are numbered from 1. *) |
| 37 |
type t = Group.t |
| 38 |
|
| 39 |
(** Raise [Not_found] if the group did not match *) |
| 40 |
val get : t -> int -> string |
| 41 |
|
| 42 |
(** Similar to {!get}, but returns an option instead of using an exception. *) |
| 43 |
val get_opt : t -> int -> string option |
| 44 |
|
| 45 |
(** Raise [Not_found] if the group did not match *) |
| 46 |
val offset : t -> int -> int * int |
| 47 |
|
| 48 |
(** Similar to {!offset}, but returns an option instead of using an exception. *) |
| 49 |
val offset_opt : t -> int -> (int * int) option |
| 50 |
|
| 51 |
(** Return the start of the match. Raise [Not_found] if the group did not match. *) |
| 52 |
val start : t -> int -> int |
| 53 |
|
| 54 |
(** Similar to {!start_opt}, but returns an option instead of using an exception. *) |
| 55 |
val start_opt : t -> int -> int option |
| 56 |
|
| 57 |
(** Return the end of the match. Raise [Not_found] if the group did not match. *) |
| 58 |
val stop : t -> int -> int |
| 59 |
|
| 60 |
(** Similar to {!stop_opt}, but returns an option instead of using an exception. *) |
| 61 |
val stop_opt : t -> int -> int option |
| 62 |
|
| 63 |
(** Return the empty string for each group which did not match *) |
| 64 |
val all : t -> string array |
| 65 |
|
| 66 |
(** Return [(-1,-1)] for each group which did not match *) |
| 67 |
val all_offset : t -> (int * int) array |
| 68 |
|
| 69 |
(** Test whether a group matched *) |
| 70 |
val test : t -> int -> bool |
| 71 |
|
| 72 |
(** Returns the total number of groups defined - matched or not. |
| 73 |
This function is experimental. *) |
| 74 |
val nb_groups : t -> int |
| 75 |
|
| 76 |
val pp : Format.formatter -> t -> unit |
| 77 |
end |
| 78 |
|
| 79 |
type groups = Group.t [@@ocaml.deprecated "Use Group.t"] |
| 80 |
|
| 81 |
(** {2 Compilation and execution of a regular expression} *) |
| 82 |
|
| 83 |
(** Compile a regular expression into an executable version that can be |
| 84 |
used to match strings, e.g. with {!exec}. *) |
| 85 |
val compile : t -> re |
| 86 |
|
| 87 |
(** Return the number of capture groups (including the one |
| 88 |
corresponding to the entire regexp). *) |
| 89 |
val group_count : re -> int |
| 90 |
|
| 91 |
(** Return named capture groups with their index. *) |
| 92 |
val group_names : re -> (string * int) list |
| 93 |
|
| 94 |
(** [exec re str] searches [str] for a match of the compiled expression [re], |
| 95 |
and returns the matched groups if any. |
| 96 |
|
| 97 |
More specifically, when a match exists, [exec] returns a match that |
| 98 |
starts at the earliest position possible. If multiple such matches are |
| 99 |
possible, the one specified by the match semantics described below is |
| 100 |
returned. |
| 101 |
|
| 102 |
{5 Examples:} |
| 103 |
{[ |
| 104 |
# let regex = Re.compile Re.(seq [str "//"; rep print ]);; |
| 105 |
val regex : re = <abstr> |
| 106 |
|
| 107 |
# Re.exec regex "// a C comment";; |
| 108 |
- : Re.Group.t = <abstr> |
| 109 |
|
| 110 |
# Re.exec regex "# a C comment?";; |
| 111 |
Exception: Not_found |
| 112 |
|
| 113 |
# Re.exec ~pos:1 regex "// a C comment";; |
| 114 |
Exception: Not_found |
| 115 |
]} |
| 116 |
|
| 117 |
@param pos optional beginning of the string (default 0) |
| 118 |
@param len |
| 119 |
length of the substring of [str] that can be matched (default [-1], |
| 120 |
meaning to the end of the string) |
| 121 |
@raise Not_found if the regular expression can't be found in [str] *) |
| 122 |
val exec |
| 123 |
: ?pos:int (** Default: 0 *) |
| 124 |
-> ?len:int (** Default: -1 (until end of string) *) |
| 125 |
-> re |
| 126 |
-> string |
| 127 |
-> Group.t |
| 128 |
|
| 129 |
(** Similar to {!exec}, but returns an option instead of using an exception. |
| 130 |
|
| 131 |
{5 Examples:} |
| 132 |
{[ |
| 133 |
# let regex = Re.compile Re.(seq [str "//"; rep print ]);; |
| 134 |
val regex : re = <abstr> |
| 135 |
|
| 136 |
# Re.exec_opt regex "// a C comment";; |
| 137 |
- : Re.Group.t option = Some <abstr> |
| 138 |
|
| 139 |
# Re.exec_opt regex "# a C comment?";; |
| 140 |
- : Re.Group.t option = None |
| 141 |
|
| 142 |
# Re.exec_opt ~pos:1 regex "// a C comment";; |
| 143 |
- : Re.Group.t option = None |
| 144 |
]} *) |
| 145 |
val exec_opt |
| 146 |
: ?pos:int (** Default: 0 *) |
| 147 |
-> ?len:int (** Default: -1 (until end of string) *) |
| 148 |
-> re |
| 149 |
-> string |
| 150 |
-> Group.t option |
| 151 |
|
| 152 |
(** Similar to {!exec}, but returns [true] if the expression matches, |
| 153 |
and [false] if it doesn't. This function is more efficient than |
| 154 |
calling {!exec} or {!exec_opt} and ignoring the returned group. |
| 155 |
|
| 156 |
{5 Examples:} |
| 157 |
{[ |
| 158 |
# let regex = Re.compile Re.(seq [str "//"; rep print ]);; |
| 159 |
val regex : re = <abstr> |
| 160 |
|
| 161 |
# Re.execp regex "// a C comment";; |
| 162 |
- : bool = true |
| 163 |
|
| 164 |
# Re.execp ~pos:1 regex "// a C comment";; |
| 165 |
- : bool = false |
| 166 |
]} *) |
| 167 |
val execp |
| 168 |
: ?pos:int (** Default: 0 *) |
| 169 |
-> ?len:int (** Default: -1 (until end of string) *) |
| 170 |
-> re |
| 171 |
-> string |
| 172 |
-> bool |
| 173 |
|
| 174 |
(** More detailed version of {!execp}. [`Full] is equivalent to [true], |
| 175 |
while [`Mismatch] and [`Partial] are equivalent to [false], but [`Partial] |
| 176 |
indicates the input string could be extended to create a match. |
| 177 |
|
| 178 |
{5 Examples:} |
| 179 |
{[ |
| 180 |
# let regex = Re.compile Re.(seq [bos; str "// a C comment"]);; |
| 181 |
val regex : re = <abstr> |
| 182 |
|
| 183 |
# Re.exec_partial regex "// a C comment here.";; |
| 184 |
- : [ `Full | `Mismatch | `Partial ] = `Full |
| 185 |
|
| 186 |
# Re.exec_partial regex "// a C comment";; |
| 187 |
- : [ `Full | `Mismatch | `Partial ] = `Partial |
| 188 |
|
| 189 |
# Re.exec_partial regex "//";; |
| 190 |
- : [ `Full | `Mismatch | `Partial ] = `Partial |
| 191 |
|
| 192 |
# Re.exec_partial regex "# a C comment?";; |
| 193 |
- : [ `Full | `Mismatch | `Partial ] = `Mismatch |
| 194 |
]} *) |
| 195 |
val exec_partial |
| 196 |
: ?pos:int (** Default: 0 *) |
| 197 |
-> ?len:int (** Default: -1 (until end of string) *) |
| 198 |
-> re |
| 199 |
-> string |
| 200 |
-> [ `Full | `Partial | `Mismatch ] |
| 201 |
|
| 202 |
(** More detailed version of {!exec_opt}. [`Full group] is equivalent to [Some group], |
| 203 |
while [`Mismatch] and [`Partial _] are equivalent to [None], but [`Partial position] |
| 204 |
indicates that the input string could be extended to create a match, and no match could |
| 205 |
start in the input string before the given position. |
| 206 |
This could be used to not have to search the entirety of the input if more |
| 207 |
becomes available, and use the given position as the [?pos] argument. *) |
| 208 |
val exec_partial_detailed |
| 209 |
: ?pos:int (** Default: 0 *) |
| 210 |
-> ?len:int (** Default: -1 (until end of string) *) |
| 211 |
-> re |
| 212 |
-> string |
| 213 |
-> [ `Full of Group.t | `Partial of int | `Mismatch ] |
| 214 |
|
| 215 |
(** Marks *) |
| 216 |
module Mark : sig |
| 217 |
(** Mark id *) |
| 218 |
type t |
| 219 |
|
| 220 |
(** Tell if a mark was matched. *) |
| 221 |
val test : Group.t -> t -> bool |
| 222 |
|
| 223 |
module Set : Set.S with type elt = t |
| 224 |
|
| 225 |
(** Return all the mark matched. *) |
| 226 |
val all : Group.t -> Set.t |
| 227 |
|
| 228 |
val equal : t -> t -> bool |
| 229 |
val compare : t -> t -> int |
| 230 |
end |
| 231 |
|
| 232 |
(** {2 High Level Operations} *) |
| 233 |
|
| 234 |
type split_token = |
| 235 |
[ `Text of string (** Text between delimiters *) |
| 236 |
| `Delim of Group.t (** Delimiter *) |
| 237 |
] |
| 238 |
|
| 239 |
(** Repeatedly calls {!exec} on the given string, starting at given position and |
| 240 |
length. |
| 241 |
|
| 242 |
{5 Examples:} |
| 243 |
{[ |
| 244 |
# let regex = Re.compile Re.(seq [str "my"; blank; word(rep alpha)]);; |
| 245 |
val regex : re = <abstr> |
| 246 |
|
| 247 |
# Re.all regex "my head, my shoulders, my knees, my toes ...";; |
| 248 |
- : Re.Group.t list = [<abstr>; <abstr>; <abstr>; <abstr>] |
| 249 |
|
| 250 |
# Re.all regex "My head, My shoulders, My knees, My toes ...";; |
| 251 |
- : Re.Group.t list = [] |
| 252 |
]} *) |
| 253 |
val all : ?pos:int -> ?len:int -> re -> string -> Group.t list |
| 254 |
|
| 255 |
type 'a gen = unit -> 'a option |
| 256 |
|
| 257 |
(** @deprecated Use {!module-Seq.all} instead. *) |
| 258 |
val all_gen : ?pos:int -> ?len:int -> re -> string -> Group.t gen |
| 259 |
[@@ocaml.deprecated "Use Seq.all"] |
| 260 |
|
| 261 |
(** @deprecated Use {!module-Seq.all} instead. *) |
| 262 |
val all_seq : ?pos:int -> ?len:int -> re -> string -> Group.t Seq.t |
| 263 |
[@@ocaml.deprecated "Use Seq.all"] |
| 264 |
|
| 265 |
(** Same as {!all}, but extracts the matched substring rather than returning |
| 266 |
the whole group. This basically iterates over matched strings. |
| 267 |
|
| 268 |
{5 Examples:} |
| 269 |
{[ |
| 270 |
# let regex = Re.compile Re.(seq [str "my"; blank; word(rep alpha)]);; |
| 271 |
val regex : re = <abstr> |
| 272 |
|
| 273 |
# Re.matches regex "my head, my shoulders, my knees, my toes ...";; |
| 274 |
- : string list = ["my head"; "my shoulders"; "my knees"; "my toes"] |
| 275 |
|
| 276 |
# Re.matches regex "My head, My shoulders, My knees, My toes ...";; |
| 277 |
- : string list = [] |
| 278 |
|
| 279 |
# Re.matches regex "my my my my head my 1 toe my ...";; |
| 280 |
- : string list = ["my my"; "my my"] |
| 281 |
|
| 282 |
# Re.matches ~pos:2 regex "my my my my head my +1 toe my ...";; |
| 283 |
- : string list = ["my my"; "my head"] |
| 284 |
]} *) |
| 285 |
val matches : ?pos:int -> ?len:int -> re -> string -> string list |
| 286 |
|
| 287 |
(** @deprecated Use {!module-Seq.matches} instead. *) |
| 288 |
val matches_gen : ?pos:int -> ?len:int -> re -> string -> string gen |
| 289 |
[@@ocaml.deprecated "Use Seq.matches"] |
| 290 |
|
| 291 |
(** @deprecated Use {!module-Seq.matches} instead. *) |
| 292 |
val matches_seq : ?pos:int -> ?len:int -> re -> string -> string Seq.t |
| 293 |
[@@ocaml.deprecated "Use Seq.matches"] |
| 294 |
|
| 295 |
(** [split re s] splits [s] into chunks separated by [re]. It yields |
| 296 |
the chunks themselves, not the separator. An occurence of the |
| 297 |
separator at the beginning or the end of the string is ignoring. |
| 298 |
|
| 299 |
{5 Examples:} |
| 300 |
{[ |
| 301 |
# let regex = Re.compile (Re.char ',');; |
| 302 |
val regex : re = <abstr> |
| 303 |
|
| 304 |
# Re.split regex "Re,Ocaml,Jerome Vouillon";; |
| 305 |
- : string list = ["Re"; "Ocaml"; "Jerome Vouillon"] |
| 306 |
|
| 307 |
# Re.split regex "No commas in this sentence.";; |
| 308 |
- : string list = ["No commas in this sentence."] |
| 309 |
|
| 310 |
# Re.split regex ",1,2,";; |
| 311 |
- : string list = ["1"; "2"] |
| 312 |
|
| 313 |
# Re.split ~pos:3 regex "1,2,3,4. Commas go brrr.";; |
| 314 |
- : string list = ["3"; "4. Commas go brrr."] |
| 315 |
]} |
| 316 |
|
| 317 |
{6 Zero-length patterns:} |
| 318 |
|
| 319 |
Be careful when using [split] with zero-length patterns like [eol], [bow], |
| 320 |
and [eow]. Because they don't have any width, they will still be present in |
| 321 |
the result. (Note the position of the [\n] and space characters in the |
| 322 |
output.) |
| 323 |
|
| 324 |
{[ |
| 325 |
# Re.split (Re.compile Re.eol) "a\nb";; |
| 326 |
- : string list = ["a"; "\nb"] |
| 327 |
|
| 328 |
# Re.split (Re.compile Re.bow) "a b";; |
| 329 |
- : string list = ["a "; "b"] |
| 330 |
|
| 331 |
# Re.split (Re.compile Re.eow) "a b";; |
| 332 |
- : string list = ["a"; " b"] |
| 333 |
]} |
| 334 |
|
| 335 |
Compare this to the behavior of splitting on the char itself. (Note that |
| 336 |
the delimiters are not present in the output.) |
| 337 |
|
| 338 |
{[ |
| 339 |
# Re.split (Re.compile (Re.char '\n')) "a\nb";; |
| 340 |
- : string list = ["a"; "b"] |
| 341 |
|
| 342 |
# Re.split (Re.compile (Re.char ' ')) "a b";; |
| 343 |
- : string list = ["a"; "b"] |
| 344 |
]} *) |
| 345 |
val split : ?pos:int -> ?len:int -> re -> string -> string list |
| 346 |
|
| 347 |
(** [split_delim re s] splits [s] into chunks separated by [re]. It |
| 348 |
yields the chunks themselves, not the separator. Occurences of the |
| 349 |
separator at the beginning or the end of the string will produce |
| 350 |
empty chunks. |
| 351 |
|
| 352 |
{5 Examples:} |
| 353 |
{[ |
| 354 |
# let regex = Re.compile (Re.char ',');; |
| 355 |
val regex : re = <abstr> |
| 356 |
|
| 357 |
# Re.split regex "Re,Ocaml,Jerome Vouillon";; |
| 358 |
- : string list = ["Re"; "Ocaml"; "Jerome Vouillon"] |
| 359 |
|
| 360 |
# Re.split regex "No commas in this sentence.";; |
| 361 |
- : string list = ["No commas in this sentence."] |
| 362 |
|
| 363 |
# Re.split regex ",1,2,";; |
| 364 |
- : string list = [""; "1"; "2"; ""] |
| 365 |
|
| 366 |
# Re.split ~pos:3 regex "1,2,3,4. Commas go brrr.";; |
| 367 |
- : string list = ["3"; "4. Commas go brrr."] |
| 368 |
]} |
| 369 |
|
| 370 |
{6 Zero-length patterns:} |
| 371 |
|
| 372 |
Be careful when using [split_delim] with zero-length patterns like [eol], |
| 373 |
[bow], and [eow]. Because they don't have any width, they will still be |
| 374 |
present in the result. (Note the position of the [\n] and space characters |
| 375 |
in the output.) |
| 376 |
|
| 377 |
{[ |
| 378 |
# Re.split_delim (Re.compile Re.eol) "a\nb";; |
| 379 |
- : string list = ["a"; "\nb"; ""] |
| 380 |
|
| 381 |
# Re.split_delim (Re.compile Re.bow) "a b";; |
| 382 |
- : string list = [""; "a "; "b"] |
| 383 |
|
| 384 |
# Re.split_delim (Re.compile Re.eow) "a b";; |
| 385 |
- : string list = ["a"; " b"; ""] |
| 386 |
]} |
| 387 |
|
| 388 |
Compare this to the behavior of splitting on the char itself. (Note that |
| 389 |
the delimiters are not present in the output.) |
| 390 |
|
| 391 |
{[ |
| 392 |
# Re.split_delim (Re.compile (Re.char '\n')) "a\nb";; |
| 393 |
- : string list = ["a"; "b"] |
| 394 |
|
| 395 |
# Re.split_delim (Re.compile (Re.char ' ')) "a b";; |
| 396 |
- : string list = ["a"; "b"] |
| 397 |
]} *) |
| 398 |
val split_delim : ?pos:int -> ?len:int -> re -> string -> string list |
| 399 |
|
| 400 |
(** @deprecated Use {!module-Seq.split} instead. *) |
| 401 |
val split_gen : ?pos:int -> ?len:int -> re -> string -> string gen |
| 402 |
[@@ocaml.deprecated "Use Seq.split"] |
| 403 |
|
| 404 |
(** @deprecated Use {!module-Seq.split} instead. *) |
| 405 |
val split_seq : ?pos:int -> ?len:int -> re -> string -> string Seq.t |
| 406 |
[@@ocaml.deprecated "Use Seq.split"] |
| 407 |
|
| 408 |
(** [split re s] splits [s] into chunks separated by [re]. It yields the chunks |
| 409 |
along with the separators. For instance this can be used with a |
| 410 |
whitespace-matching re such as ["[\t ]+"]. |
| 411 |
|
| 412 |
{5 Examples:} |
| 413 |
{[ |
| 414 |
# let regex = Re.compile (Re.char ',');; |
| 415 |
val regex : re = <abstr> |
| 416 |
|
| 417 |
# Re.split_full regex "Re,Ocaml,Jerome Vouillon";; |
| 418 |
- : Re.split_token list = |
| 419 |
[`Text "Re"; `Delim <abstr>; `Text "Ocaml"; `Delim <abstr>; |
| 420 |
`Text "Jerome Vouillon"] |
| 421 |
|
| 422 |
# Re.split_full regex "No commas in this sentence.";; |
| 423 |
- : Re.split_token list = [`Text "No commas in this sentence."] |
| 424 |
|
| 425 |
# Re.split_full ~pos:3 regex "1,2,3,4. Commas go brrr.";; |
| 426 |
- : Re.split_token list = |
| 427 |
[`Delim <abstr>; `Text "3"; `Delim <abstr>; `Text "4. Commas go brrr."] |
| 428 |
]} *) |
| 429 |
val split_full : ?pos:int -> ?len:int -> re -> string -> split_token list |
| 430 |
|
| 431 |
(** @deprecated Use {!module-Seq.split_full} instead. *) |
| 432 |
val split_full_gen : ?pos:int -> ?len:int -> re -> string -> split_token gen |
| 433 |
[@@ocaml.deprecated "Use Seq.split_full"] |
| 434 |
|
| 435 |
(** @deprecated Use {!module-Seq.split_full} instead. *) |
| 436 |
val split_full_seq : ?pos:int -> ?len:int -> re -> string -> split_token Seq.t |
| 437 |
[@@ocaml.deprecated "Use Seq.split_full"] |
| 438 |
|
| 439 |
module Seq : sig |
| 440 |
(** Same as {!module-Re.val-all} but returns an iterator. |
| 441 |
|
| 442 |
{5 Examples:} |
| 443 |
{[ |
| 444 |
# let regex = Re.compile Re.(seq [str "my"; blank; word(rep alpha)]);; |
| 445 |
val regex : re = <abstr> |
| 446 |
|
| 447 |
# Re.Seq.all regex "my head, my shoulders, my knees, my toes ...";; |
| 448 |
- : Re.Group.t Seq.t = <fun> |
| 449 |
]} |
| 450 |
@since 1.10.0 *) |
| 451 |
val all : ?pos:int (** Default: 0 *) -> ?len:int -> re -> string -> Group.t Seq.t |
| 452 |
|
| 453 |
(** Same as {!module-Re.val-matches}, but returns an iterator. |
| 454 |
|
| 455 |
{5 Example:} |
| 456 |
{[ |
| 457 |
# let regex = Re.compile Re.(seq [str "my"; blank; word(rep alpha)]);; |
| 458 |
val regex : re = <abstr> |
| 459 |
|
| 460 |
# Re.Seq.matches regex "my head, my shoulders, my knees, my toes ...";; |
| 461 |
- : string Seq.t = <fun> |
| 462 |
]} |
| 463 |
@since 1.10.0 *) |
| 464 |
val matches : ?pos:int (** Default: 0 *) -> ?len:int -> re -> string -> string Seq.t |
| 465 |
|
| 466 |
(** Same as {!module-Re.val-split} but returns an iterator. |
| 467 |
|
| 468 |
{5 Example:} |
| 469 |
{[ |
| 470 |
# let regex = Re.compile (Re.char ',');; |
| 471 |
val regex : re = <abstr> |
| 472 |
|
| 473 |
# Re.Seq.split regex "Re,Ocaml,Jerome Vouillon";; |
| 474 |
- : string Seq.t = <fun> |
| 475 |
]} |
| 476 |
@since 1.10.0 *) |
| 477 |
val split : ?pos:int (** Default: 0 *) -> ?len:int -> re -> string -> string Seq.t |
| 478 |
|
| 479 |
(** Same as {!module-Re.val-split_delim} but returns an iterator. |
| 480 |
|
| 481 |
{5 Example:} |
| 482 |
{[ |
| 483 |
# let regex = Re.compile (Re.char ',');; |
| 484 |
val regex : re = <abstr> |
| 485 |
|
| 486 |
# Re.Seq.split regex "Re,Ocaml,Jerome Vouillon";; |
| 487 |
- : string Seq.t = <fun> |
| 488 |
]} |
| 489 |
@since 1.11.1 *) |
| 490 |
val split_delim : ?pos:int (** Default: 0 *) -> ?len:int -> re -> string -> string Seq.t |
| 491 |
|
| 492 |
(** Same as {!module-Re.val-split_full} but returns an iterator. |
| 493 |
|
| 494 |
{5 Example:} |
| 495 |
{[ |
| 496 |
# let regex = Re.compile (Re.char ',');; |
| 497 |
val regex : re = <abstr> |
| 498 |
|
| 499 |
# Re.Seq.split_full regex "Re,Ocaml,Jerome Vouillon";; |
| 500 |
- : Re.split_token Seq.t = <fun> |
| 501 |
]} |
| 502 |
@since 1.10.0 *) |
| 503 |
val split_full |
| 504 |
: ?pos:int (** Default: 0 *) |
| 505 |
-> ?len:int |
| 506 |
-> re |
| 507 |
-> string |
| 508 |
-> split_token Seq.t |
| 509 |
end |
| 510 |
|
| 511 |
(** {2 String expressions (literal match)} *) |
| 512 |
|
| 513 |
val str : string -> t |
| 514 |
val char : char -> t |
| 515 |
|
| 516 |
(** {2 Basic operations on regular expressions} *) |
| 517 |
|
| 518 |
(** Alternative. |
| 519 |
|
| 520 |
[alt []] is equivalent to {!empty}. |
| 521 |
|
| 522 |
By default, the leftmost match is preferred (see match semantics below). *) |
| 523 |
val alt : t list -> t |
| 524 |
|
| 525 |
(** Sequence *) |
| 526 |
val seq : t list -> t |
| 527 |
|
| 528 |
(** Match nothing *) |
| 529 |
val empty : t |
| 530 |
|
| 531 |
(** Empty word *) |
| 532 |
val epsilon : t |
| 533 |
|
| 534 |
(** 0 or more matches *) |
| 535 |
val rep : t -> t |
| 536 |
|
| 537 |
(** 1 or more matches *) |
| 538 |
val rep1 : t -> t |
| 539 |
|
| 540 |
(** [repn re i j] matches [re] at least [i] times |
| 541 |
and at most [j] times, bounds included. |
| 542 |
[j = None] means no upper bound. *) |
| 543 |
val repn : t -> int -> int option -> t |
| 544 |
|
| 545 |
(** 0 or 1 matches *) |
| 546 |
val opt : t -> t |
| 547 |
|
| 548 |
(** {2 String, line, word} |
| 549 |
|
| 550 |
We define a word as a sequence of latin1 letters, digits and underscore. *) |
| 551 |
|
| 552 |
(** Beginning of line *) |
| 553 |
val bol : t |
| 554 |
|
| 555 |
(** End of line *) |
| 556 |
val eol : t |
| 557 |
|
| 558 |
(** Beginning of word *) |
| 559 |
val bow : t |
| 560 |
|
| 561 |
(** End of word *) |
| 562 |
val eow : t |
| 563 |
|
| 564 |
(** Beginning of string. This differs from {!start} because it matches |
| 565 |
the beginning of the input string even when using [~pos] arguments: |
| 566 |
|
| 567 |
{[ |
| 568 |
let b = execp (compile (seq [ bos; str "a" ])) "aa" ~pos:1 in |
| 569 |
assert (not b) |
| 570 |
]} *) |
| 571 |
val bos : t |
| 572 |
|
| 573 |
(** End of string. This is different from {!stop} in the way described |
| 574 |
in {!bos}. *) |
| 575 |
val eos : t |
| 576 |
|
| 577 |
(** Last end of line or end of string *) |
| 578 |
val leol : t |
| 579 |
|
| 580 |
(** Initial position. This differs from {!bos} because it takes into |
| 581 |
account the [~pos] arguments: |
| 582 |
|
| 583 |
{[ |
| 584 |
let b = execp (compile (seq [ start; str "a" ])) "aa" ~pos:1 in |
| 585 |
assert b |
| 586 |
]} *) |
| 587 |
val start : t |
| 588 |
|
| 589 |
(** Final position. This is different from {!eos} in the way described |
| 590 |
in {!start}. *) |
| 591 |
val stop : t |
| 592 |
|
| 593 |
(** Word *) |
| 594 |
val word : t -> t |
| 595 |
|
| 596 |
(** Not at a word boundary *) |
| 597 |
val not_boundary : t |
| 598 |
|
| 599 |
(** Only matches the whole string, i.e. [fun t -> seq [ bos; t; eos ]]. *) |
| 600 |
val whole_string : t -> t |
| 601 |
|
| 602 |
(** {2 Match semantics} |
| 603 |
|
| 604 |
A regular expression frequently matches a string in multiple ways. For |
| 605 |
instance [exec (compile (opt (str "a"))) "ab"] can match "" or "a". Match |
| 606 |
semantic can be modified with the functions below, allowing one to choose |
| 607 |
which of these is preferable. |
| 608 |
|
| 609 |
By default, the leftmost branch of alternations is preferred, and repetitions |
| 610 |
are greedy. |
| 611 |
|
| 612 |
Note that the existence of matches cannot be changed by specifying match |
| 613 |
semantics. [seq [ bos; str "a"; non_greedy (opt (str "b")); eos ]] will |
| 614 |
match when applied to "ab". However if [seq [ bos; str "a"; non_greedy (opt |
| 615 |
(str "b")) ]] is applied to "ab", it will match "a" rather than "ab". |
| 616 |
|
| 617 |
Also note that multiple match semantics can conflict. In this case, the one |
| 618 |
executed earlier takes precedence. For instance, any match of [shortest (seq |
| 619 |
[ bos; group (rep (str "a")); group (rep (str "a")); eos ])] will always have |
| 620 |
an empty first group. Conversely, if we use [longest] instead of [shortest], |
| 621 |
the second group will always be empty. *) |
| 622 |
|
| 623 |
(** Longest match semantics. That is, matches will match as many bytes as |
| 624 |
possible. If multiple choices match the maximum amount of bytes, the one |
| 625 |
respecting the inner match semantics is preferred. *) |
| 626 |
val longest : t -> t |
| 627 |
|
| 628 |
(** Same as {!longest}, but matching the least number of bytes. *) |
| 629 |
val shortest : t -> t |
| 630 |
|
| 631 |
(** First match semantics for alternations (not repetitions). That is, matches |
| 632 |
will prefer the leftmost branch of the alternation that matches the text. *) |
| 633 |
val first : t -> t |
| 634 |
|
| 635 |
(** Greedy matches for repetitions ({!opt}, {!rep}, {!rep1}, {!repn}): they will |
| 636 |
match as many times as possible. *) |
| 637 |
val greedy : t -> t |
| 638 |
|
| 639 |
(** Non-greedy matches for repetitions ({!opt}, {!rep}, {!rep1}, {!repn}): they |
| 640 |
will match as few times as possible. *) |
| 641 |
val non_greedy : t -> t |
| 642 |
|
| 643 |
(** {2 Groups (or submatches)} *) |
| 644 |
|
| 645 |
(** Delimit a group. The group is considered as matching if it is used at least |
| 646 |
once (it may be used multiple times if is nested inside {!rep} for |
| 647 |
instance). If it is used multiple times, the last match is what gets |
| 648 |
captured. *) |
| 649 |
val group : ?name:string -> t -> t |
| 650 |
|
| 651 |
(** Remove all groups *) |
| 652 |
val no_group : t -> t |
| 653 |
|
| 654 |
(** When matching against [nest e], only the group matching in the |
| 655 |
last match of e will be considered as matching. |
| 656 |
|
| 657 |
For instance: |
| 658 |
{[ |
| 659 |
let re = compile (rep1 (nest (alt [ group (str "a"); str "b" ]))) in |
| 660 |
let group = Re.exec re "ab" in |
| 661 |
assert (Group.get_opt group 1 = None); |
| 662 |
(* same thing but without [nest] *) |
| 663 |
let re = compile (rep1 (alt [ group (str "a"); str "b" ])) in |
| 664 |
let group = Re.exec re "ab" in |
| 665 |
assert (Group.get_opt group 1 = Some "a") |
| 666 |
]} *) |
| 667 |
val nest : t -> t |
| 668 |
|
| 669 |
(** Mark a regexp. the markid can then be used to know if this regexp was used. *) |
| 670 |
val mark : t -> Mark.t * t |
| 671 |
|
| 672 |
(** {2 Character sets} *) |
| 673 |
|
| 674 |
(** Any character of the string *) |
| 675 |
val set : string -> t |
| 676 |
|
| 677 |
(** Character ranges *) |
| 678 |
val rg : char -> char -> t |
| 679 |
|
| 680 |
(** Intersection of character sets *) |
| 681 |
val inter : t list -> t |
| 682 |
|
| 683 |
(** Difference of character sets *) |
| 684 |
val diff : t -> t -> t |
| 685 |
|
| 686 |
(** Complement of union *) |
| 687 |
val compl : t list -> t |
| 688 |
|
| 689 |
(** {2 Predefined character sets} *) |
| 690 |
|
| 691 |
(** Any character *) |
| 692 |
val any : t |
| 693 |
|
| 694 |
(** Any character but a newline *) |
| 695 |
val notnl : t |
| 696 |
|
| 697 |
val alnum : t |
| 698 |
val wordc : t |
| 699 |
val alpha : t |
| 700 |
val ascii : t |
| 701 |
val blank : t |
| 702 |
val cntrl : t |
| 703 |
val digit : t |
| 704 |
val graph : t |
| 705 |
val lower : t |
| 706 |
val print : t |
| 707 |
val punct : t |
| 708 |
val space : t |
| 709 |
val upper : t |
| 710 |
val xdigit : t |
| 711 |
|
| 712 |
(** {2 Case modifiers} *) |
| 713 |
|
| 714 |
(** Case sensitive matching. Note that this works on latin1, not ascii and not |
| 715 |
utf8. *) |
| 716 |
val case : t -> t |
| 717 |
|
| 718 |
(** Case insensitive matching. Note that this works on latin1, not ascii and not |
| 719 |
utf8. *) |
| 720 |
val no_case : t -> t |
| 721 |
|
| 722 |
(****) |
| 723 |
|
| 724 |
(** {2 Internal debugging} *) |
| 725 |
|
| 726 |
val pp : Format.formatter -> t -> unit |
| 727 |
val pp_re : Format.formatter -> re -> unit |
| 728 |
|
| 729 |
(** Alias for {!pp_re}. Deprecated *) |
| 730 |
val print_re : Format.formatter -> re -> unit |
| 731 |
|
| 732 |
(** {2 Experimental functions} *) |
| 733 |
|
| 734 |
(** [witness r] generates a string [s] such that [execp (compile r) s] is true. |
| 735 |
|
| 736 |
Be warned that this function is buggy because it ignores zero-width |
| 737 |
assertions like beginning of words. As a result it can generate incorrect |
| 738 |
results. *) |
| 739 |
val witness : t -> string |
| 740 |
|
| 741 |
(** {2 Deprecated functions} *) |
| 742 |
|
| 743 |
(** Alias for {!Group.t}. Deprecated *) |
| 744 |
type substrings = Group.t [@@ocaml.deprecated "Use Group.t"] |
| 745 |
|
| 746 |
(** Same as {!Group.get}. Deprecated *) |
| 747 |
val get : Group.t -> int -> string |
| 748 |
[@@ocaml.deprecated "Use Group.get"] |
| 749 |
|
| 750 |
(** Same as {!Group.offset}. Deprecated *) |
| 751 |
val get_ofs : Group.t -> int -> int * int |
| 752 |
[@@ocaml.deprecated "Use Group.offset"] |
| 753 |
|
| 754 |
(** Same as {!Group.all}. Deprecated *) |
| 755 |
val get_all : Group.t -> string array |
| 756 |
[@@ocaml.deprecated "Use Group.all"] |
| 757 |
|
| 758 |
(** Same as {!Group.all_offset}. Deprecated *) |
| 759 |
val get_all_ofs : Group.t -> (int * int) array |
| 760 |
[@@ocaml.deprecated "Use Group.all_offset"] |
| 761 |
|
| 762 |
(** Same as {!Group.test}. Deprecated *) |
| 763 |
val test : Group.t -> int -> bool |
| 764 |
[@@ocaml.deprecated "Use Group.test"] |
| 765 |
|
| 766 |
(** Alias for {!Mark.t}. Deprecated *) |
| 767 |
type markid = Mark.t [@@ocaml.deprecated "Use Mark."] |
| 768 |
|
| 769 |
(** Same as {!Mark.test}. Deprecated *) |
| 770 |
val marked : Group.t -> Mark.t -> bool |
| 771 |
[@@ocaml.deprecated "Use Mark.test"] |
| 772 |
|
| 773 |
(** Same as {!Mark.all}. Deprecated *) |
| 774 |
val mark_set : Group.t -> Mark.Set.t |
| 775 |
[@@ocaml.deprecated "Use Mark.all"] |