A typed regular expression.
The type variable is the type of the returned value when the typed regular expression (tyregex) is executed.
For example tyre : int t can be used to return an int. In the rest of the documentation, we will use «tyre» to designate a value of type t.
Combinators
val pcre : string -> string tpcre s is a tyregex that matches the PCRE s and return the corresponding string. Groups in s are ignored.
regex re is a tyregex that matches re and return the corresponding string. Groups inside re are erased.
conv to_ from_ tyre matches the same text as tyre, but converts back and forth to a different data type.
to_ is allowed to raise an exception exn. In this case, exec will return `ConverterFailure exn.
For example, this is the implementation of pos_int:
let pos_int =
Tyre.conv
int_of_string string_of_int
(Tyre.regex (Re.rep1 Re.digit))alt tyreL tyreR matches either tyreL (and will then return `Left v) or tyreR (and will then return `Right v).
Repetitions
val rep : 'a t -> 'a Stdlib.Seq.t tval rep1 : 'a t -> ('a * 'a Stdlib.Seq.t) trep1 tyre is seq tyre (rep tyre). Similar to Re.rep1.
Sequences
seq tyre1 tyre2 matches tyre1 then tyre2 and return both values.
prefix tyre_i tyre matches tyre_i, ignores the result, and then matches tyre and returns its result. Converters in tyre_i are never called.
Infix operators
module Infix : sig ... endUseful combinators
val str : string -> unit tstr s matches s and evaluates to s.
val char : char -> unit tchar c matches c and evaluates to c.
val blanks : unit tblanks matches Re.(rep blank) and doesn't return anything.
val int : int tint matches -?[0-9]+ and returns the matched integer.
Integers that do not fit in an int will fail.
val pos_int : int tpos_int matches [0-9]+ and returns the matched positive integer.
Integers that do not fit in an int will fail.
val float : float tfloat matches -?[0-9]+( .[0-9]* )? and returns the matched floating point number.
Floating point numbers that do not fit in a float returns infinity or neg_infinity.
val bool : bool tbool matches true|false and returns the matched boolean.
separated_list ~sep tyre is equivalent to opt (e <&> list (sep *> e)).
Other combinators
val start : unit tval stop : unit tMatching
val pp_error : Stdlib.Format.formatter -> _ error -> unitval exec : ?pos:int -> ?len:int -> 'a re -> string -> ('a, 'a error) Result.resultexec ctyre s matches the string s using the compiled tyregex ctyre and returns the extracted value.
Returns Error (`NoMatch (tyre, s) if tyre doesn't match s. Returns Error (`ConverterFailure exn) if a converter failed with the exception exn.
- parameter pos
Optional beginning of the string (default 0)
- parameter len
Length of the substring of
strthat can be matched (default to the end of the string)
val execp : ?pos:int -> ?len:int -> 'a re -> string -> boolexecp ctyre s returns true if ctyre matches s. Converters are never called.
- parameter pos
Optional beginning of the string (default 0)
- parameter len
Length of the substring of
strthat can be matched (default to the end of the string)
- since
- 0.1.1
Repeated Matching
val all : ?pos:int -> ?len:int -> 'a re -> string -> ('a list, 'a error) Result.resultall ctyre s calls to exec repeatedly and returns the list of all the matches.
val all_seq : ?pos:int -> ?len:int -> 'a re -> string -> 'a Stdlib.Seq.tall_seq ctyre s is all ctyre s but returns a gen instead. Matches are enumerated lazily.
Exceptions raised by converters are not caught.
Routing
type +'a route = | Route : 'x t * ('x -> 'a) -> 'a route | A route is a pair of a tyregex and a handler. When the tyregex is matched, the function is called with the result of the matching. |
route [ tyre1 --> f1 ; tyre2 --> f2 ] produces a compiled tyregex such that, if tyre1 matches, f1 is called, and so on.
The compiled tyregex shoud be used with exec.
Evaluating
val eval : 'a t -> 'a -> stringeval tyre v returns a string s such that exec (compile tyre) s = v.
Note that such string s is not unique. eval will usually returns a very simple witness.
val evalpp : 'a t -> Stdlib.Format.formatter -> 'a -> unitevalpp tyre ppf v is equivalent to Format.fprintf ppf "%s" (eval tyre v), but more efficient.
Is is generally used with "%a":
let my_pp = Tyre.evalpp tyre in
Format.printf "%a@." my_pp vPretty printing
val pp : Stdlib.Format.formatter -> 'a t -> unitval pp_re : Stdlib.Format.formatter -> 'a re -> unit