Re: "ocaml_beginners"::[] Get all w ords between two markers in a string
"Markus Weißmann [email protected] [ocaml_beginners]" <[email protected]> Fri, 05 Aug 2016 15:18:30 +0200
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
On 2016-07-15 10:57, [email protected] [ocaml_beginners] wrote: > I need to get the list of all value names in a given module. > So I put the contents of the mli in a string, and now I need > to do some regexp manipulation on it, selecting all > words between "val" and ":" that's the part where > I'm stuck. > > So what would be nice here is a grab_value_names:string->string > function, such that > > grab_value_names "**** val get : ***** val set : **** val make : ***" > > returns ["get";"set";"make"]. > > > I'm not sure about how to do this with the Str module. I thought > first > of cleaning everything between a ":" and "val" ; I tried the > following > which doesn't work : > > let cleanup s=Str.global_replace (Str.regexp ":[.]+val") ":\nval" s;; The best "hack" solution (that is w/o using a real parser of the real grammar), could be something like this: 1. Split the contents by whitespace let words = Str.split (Str.regexp "[ \t]+") file_contents 2. Fold over this string list, searching for "val"/something/":" sequences let check (had_val, had_str, names) str = match (str, had_val, had_str) with | (":", true, Some name) -> (false, None, name::names) (* saw a "val" and a name-string, now the ":"; add name to names, reset iterator *) | ("val", _, _) -> (true, None, names) (* just saw a "val" *) | (s, true, None) -> (true, Some s, names) (* saw a name-string after seeing a "val"; remember that string *) | _ -> (false, None, names) (* throw away everything else, reset the iterator *) let _, _, names = List.fold_left check (false, None, []) words The above code is not tested in any way. ;) regards Markus -- Markus Weißmann, M.Sc. Technische Universität München Institut für Informatik Boltzmannstr. 3 D-85748 Garching Germany http://wwwknoll.in.tum.de/