Re: Re: Matching numbers at the beginning of a string?
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAMu2m2KO7=fuJeV335hDNm+RCyN-gpbaxfw4o+TgvfpS3REJKw@mail.gmail.com> |
There is also ocaml-pcre [1], but re is probably a better choice since it is pure OCaml and supposedly faster. [1] https://github.com/mmottl/pcre-ocaml On Fri, Nov 20, 2015 at 12:23 AM, [email protected] [ocaml_beginners] < [email protected]> wrote: > > > > The only perl style regular expression library I know of is "re" - > https://opam.ocaml.org/packages/re/re.1.4.1/ - third-party OCaml library. > > Alternatively, if you don't want to use a third-party library - below is a > rough implementation of your problem. The function will search the string > removing any non-digit character and get the length of the returned string. > I'm not sure what your input requirements are, but based on you original > post example - I assume something like - "122323". > > houdeshb@BMacBook-Pro:~$ ocaml > OCaml version 4.02.3 > > # #load "str.cma";; > # let search_digits (str:string) (len:int) = > let remove_nondigit = Str.global_replace (Str.regexp "[^0-9]+") "" str in > if ((String.length remove_nondigit) <> len) then false else true > ;; > val search_digits : string -> int -> bool = <fun> > # search_digits "123333sdsds" 6;; > - : bool = true > # search_digits "12333232322" 6;; > - : bool = false > # search_digits "123333" 6;; > - : bool = true > # search_digits "12333345" 8;; > - : bool = true > # > > > > >