Re: Matching numbers at the beginning of a string?
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
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
#