Get all regexp matches from a string

So the Str regexp is a nightmare :laughing:

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. :neutral_face:

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 -> []
    in
    let all_matches = get_all_matches 0 in ...

Note that 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.

Cheers,
Nicolas

1 Like

Thank you, that’s what people recommended on the IRC too, but it had no docs page. :frowning:

What do you mean ? Both docs on ocaml.org and 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.

From the readme:

Bug reports, suggestions and contributions are welcome.

:wink:

2 Likes