module Xstr_match:Copyright 1999 by Gerd Stolpmannsig
..end
type
variable
type
charset
type
matcher =
| |
Literal of |
| |
Anystring |
| |
Lazystring |
| |
Anychar |
| |
Anystring_from of |
| |
Lazystring_from of |
| |
Anychar_from of |
| |
Nullstring |
| |
Alternative of |
| |
Optional of |
| |
Record of |
| |
Scanner of |
ml1; ml2; ...
first tries the sequence ml1, then ml2, and so on
until one of the sequences leads to a match of the
whole string
Optional ml: first tries the sequence ml, them the empty string.
= Alternative ml; [Nullstring]
Record (v, ml): matches the same as ml, but the region of the string
is recorded in v
Scanner f: f s is called where s is the rest to match. The function
should return the number of characters it can match,
or raise Not_foundval match_string : matcher list -> string -> bool
type
replacer =
| |
ReplaceLiteral of |
| |
ReplaceVar of |
| |
ReplaceFunction of |
type
rflag =
| |
Anchored |
|||
| |
Limit of |
(* | | RightToLeft | *) |
val replace_matched_substrings : matcher list ->
replacer list -> rflag list -> string -> string * int
All substrings of 's' are matched against 'ml' in turn, and all non-overlapping matchings are replaced according 'rl'. The standard behaviour is to test from left to right, and to replace all occurences of substrings. This can be modified by 'fl':
val var : string -> variable
Note thread-safety: variables must not be shared by multiple threads.
val var_matched : variable -> bool
val string_of_var : variable -> string
val found_string_of_var : variable -> string
val mkset : string -> charset
val mknegset : string -> charset
let v = var "" in
let _ = match_string Literal "("; Record (v, [Anystring]); Literal ")"
s
in found_string_of_var v
let v = var "" in
let _ = match_string Lazystring;
Literal "("; Record (v, [Lazystring]); Literal ")";
Anystring
s
in found_string_of_var v
VARIANT II:
let v = var "" in
let _ = match_string Lazystring;
Literal "("; Record (v, [Anystring]); Literal ")";
Anystring
s
in found_string_of_var v
let v = var "" in
let digits = mkset "0-9" in
let digits_re = Record(v, [ Anychar_from digits; Anystring_from digits])
in
replace_matched_substrings digits_re ReplaceLiteral "D"
[] "ab012cd456fg"
yields: ("abDcdDfg", 2)
VARIANT I:
replace_matched_substrings digits_re ReplaceLiteral "D"
Limit 1
"ab012cd456fg"
yields: ("abDcd456fg", 1)
VARIANT II:
replace_matched_substrings digits_re ReplaceLiteral "D"
Anchored
"ab012cd456fg"
yields: ("ab012cd456fg", 0)
VARIANT III:
replace_matched_substrings digits_re ReplaceLiteral "D"
Anchored
"012"
yields: ("D", 1)
VARIANT IV:
let f() = string_of_int(1+int_of_string(string_of_var v)) in
replace_matched_substrings digits_re ReplaceFunction f
[] "ab012cd456fg"
yields: ("ab13cd457fg", 2)