Compilation and execution of a regular expression
Compile a regular expression into an executable version that can be used to match strings, e.g. with exec.
exec re str matches str against the compiled expression re, and returns the matched groups if any.
- parameter pos
optional beginning of the string (default 0)
- parameter len
length of the substring of
strthat can be matched (default-1, meaning to the end of the string
- raises Not_found
if the regular expression can't be found in
str
Similar to exec, but returns an option instead of using an exception.
val execp : ?pos:int -> ?len:int -> re -> string -> boolSimilar to exec, but returns true if the expression matches, and false if it doesn't
val exec_partial : ?pos:int -> ?len:int -> re -> string -> [ `Full | `Partial | `Mismatch ]More detailed version of exec_p
module Group : sig ... endManipulate matching groups.
module Mark : sig ... endMarks
High Level Operations
Repeatedly calls exec on the given string, starting at given position and length.
Same as all but returns a generator
val matches : ?pos:int -> ?len:int -> re -> string -> string listSame as all, but extracts the matched substring rather than returning the whole group. This basically iterates over matched strings
Same as matches, but returns a generator.
val split : ?pos:int -> ?len:int -> re -> string -> string listsplit re s splits s into chunks separated by re. It yields the chunks themselves, not the separator. For instance this can be used with a whitespace-matching re such as "[\t ]+".
val split_full : ?pos:int -> ?len:int -> re -> string -> split_token listval split_full_gen : ?pos:int -> ?len:int -> re -> string -> split_token genval replace : ?pos:int -> ?len:int -> ?all:bool -> re -> f:(Group.t -> string) -> string -> stringreplace ~all re ~f s iterates on s, and replaces every occurrence of re with f substring where substring is the current match. If all = false, then only the first occurrence of re is replaced.
val replace_string : ?pos:int -> ?len:int -> ?all:bool -> re -> by:string -> string -> stringreplace_string ~all re ~by s iterates on s, and replaces every occurrence of re with by. If all = false, then only the first occurrence of re is replaced.
String expressions (literal match)
val str : string -> tval char : char -> tBasic operations on regular expressions
val empty : tMatch nothing
val epsilon : tEmpty word
repn re i j matches re at least i times and at most j times, bounds included. j = None means no upper bound.
String, line, word
val bol : tBeginning of line
val eol : tEnd of line
val bow : tBeginning of word
val eow : tEnd of word
val bos : tBeginning of string
val eos : tEnd of string
val leol : tLast end of line or end of string
val start : tInitial position
val stop : tFinal position
val not_boundary : tNot at a word boundary
Match semantics
Repeated match modifiers
Groups (or submatches)
when matching against nest e, only the group matching in the last match of e will be considered as matching
Mark a regexp. the markid can then be used to know if this regexp was used.
Character sets
val set : string -> tAny character of the string
val rg : char -> char -> tCharacter ranges
Predefined character sets
val any : tAny character
val notnl : tAny character but a newline
val alnum : tval wordc : tval alpha : tval ascii : tval blank : tval cntrl : tval digit : tval graph : tval lower : tval print : tval punct : tval space : tval upper : tval xdigit : tCase modifiers
Internal debugging
val pp : Stdlib.Format.formatter -> t -> unitval pp_re : Stdlib.Format.formatter -> re -> unitval print_re : Stdlib.Format.formatter -> re -> unitAlias for pp_re. Deprecated
Experimental functions
val witness : t -> stringwitness r generates a string s such that execp (compile r) s is true
Deprecated functions
val get_ofs : Group.t -> int -> int * intSame as Group.offset. Deprecated
val get_all_ofs : Group.t -> (int * int) arraySame as Group.all_offset. Deprecated
val test : Group.t -> int -> boolSame as Group.test. Deprecated
val mark_set : Group.t -> Mark.Set.tSame as Mark.all. Deprecated