Why would Str.matched_string throw “Invalid_argument Str.matched_group”???
I’m trying to write a recursive method to get all regexp matches as a list from a string. Amazingly, there’s no such core library function.
Edit: Huh. For some reason, I only get that error when doing a match Str.matched_string s with ..., and not when doing let m = Str.matched_string s in .... Weird.
let s = Str.global_replace (Str.regexp "%%") "" s in
let regexp = Str.regexp "%[sd]" in
let rec get_all_matches i = match Str.search_forward regexp s i with
| i -> Str.matched_string s :: get_all_matches (i + 1)
| exception Not_found -> []
in
let all_matches = get_all_matches 0 in ...