So the Str regexp is a nightmare
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.
One solution, for future me.
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 -> 
let all_matches = get_all_matches 0 in ...
str is old and essentially deprecated. I would suggest you try a more recent library, eg
GitHub - ocaml/ocaml-re: Pure OCaml regular expressions, with support for Perl and POSIX-style strings.
Thank you, that’s what people recommended on the IRC too, but it had no docs page.
but it had no docs page.
What do you mean ? Both docs on
odig doc re show a reasonably documented library no ?
There have been some issues with
ocaml.org documentations just now, but that particular package seems to be working again indeed: Documentation for (most/lots of) packages are 404 · Issue #560 · ocaml/ocaml.org · GitHub.
As well as no link from their Github page.