package re2

  1. Overview
  2. Docs
module type S = sig ... end
include S
type 'a t

A value of type 'a t is a regex that parses 'as. The matching is implemented using Re2.

UTF-8 is supported by Re2 but not by this module. This is because we want to use char as a character type, but that's just wrong in a multibyte encoding.

val sexp_of_t : ('a -> Ppx_sexp_conv_lib.Sexp.t) -> 'a t -> Ppx_sexp_conv_lib.Sexp.t
val compile : ?case_sensitive:bool -> 'a t -> (string -> 'a option) Core_kernel.Staged.t

case_sensitive defaults to true.

val run : ?case_sensitive:bool -> 'a t -> string -> 'a option
val matches : ?case_sensitive:bool -> 'a t -> string -> bool
val to_regex_string : _ t -> string

to_regex_string and to_re2 both forget what a 'a t knows about turning the matching strings into 'as

val to_re2 : ?case_sensitive:bool -> _ t -> Regex.t

The applicative interface provides sequencing, e.g. both a b is a regex that parses an a followed by a b and returns both results in a pair.

include Core_kernel.Applicative.S with type 'a t := 'a t
val return : 'a -> 'a t
val apply : ('a -> 'b) t -> 'a t -> 'b t
val map : 'a t -> f:('a -> 'b) -> 'b t
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
val map3 : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t
val all : 'a t list -> 'a list t
val all_unit : unit t list -> unit t
val all_ignore : unit t list -> unit t
  • deprecated [since 2018-02] Use [all_unit]
val both : 'a t -> 'b t -> ('a * 'b) t
module Applicative_infix : sig ... end
include module type of Applicative_infix
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

same as apply

val (<*) : 'a t -> unit t -> 'a t
val (*>) : unit t -> 'a t -> 'a t
val of_re2 : Regex.t -> string option array t

of_re2 r forgets the options that r was compiled with, instead using `Encoding_latin1 true, `Dot_nl true, and the case-sensitivity setting of the overall pattern. You can still try and use '(?flags:re)' Re2 syntax to set options for the scope of this regex.

The returned values are precisely the captures of the underlying regex, in order: note that unlike (say) Re2.Match.get_all, the whole match is *not* included (if you want that, just use capture). Named captures are not accessible by name.

val ignore : _ t -> unit t

ignore t is a regex which matches the same strings that t matches, but doesn't call functions on the captured submatches. Particularly, something like ignore (map (string "x") ~f:Int.of_string) won't raise an exception, because the int conversion is never attempted.

val capture : unit t -> string t

capture t returns the string matched by t

val and_capture : 'a t -> ('a * string) t

and_capture t returns the string matched by t in addition to whatever it was already going to return.

val fail : 'a t

Regex that matches nothing

val or_ : 'a t list -> 'a t
val optional : ?greedy:bool -> 'a t -> 'a option t

greedy defaults to true. If false, the regexp will prefer not matching.

val repeat : ?greedy:bool -> ?min:int -> ?max:int option -> unit t -> unit t

repeat ~min ~max t constructs the regex t{min,max}. min defaults to 0 and max defaults to None (unbounded), so that just plain repeat t is equivalent to t*.

It would be better for repeat to be 'a t -> 'a list t, but the re2 library doesn't give you access to repeated submatches like that. Hence, repeat ignores all submatches of its argument and does not call any callbacks that may have been attached to them, as if it had ignore called on its result.

val times : unit t -> int -> unit t

times r n essentially constructs the regex r{n}. It is equivalent to repeat ~min:n ~max:(Some n) r.

Compare with, say, all (List.init n ~f:(fun _ -> r)), which constructs the regex rrr...r (with n copies of r) and has the type 'a t -> 'a list t.

val string : string -> unit t
val any_string : string t
val start_of_input : unit t

Matches empty string at the beginning of the text

val end_of_input : unit t

Matches empty string at the end of the text

module Char : sig ... end
module Decimal : sig ... end
module Let_syntax : sig ... end
OCaml

Innovation. Community. Security.