Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
integer regular expressions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] integer regular expressions wrote:
> Hello,
> I'm looking for a way to match integer numbers (immediate values), whose
> types may be either int, int32, int64 or big_int. A conversion to string
> in order to use string (perl compatible) regular expressions is likely
> not the solution I'm looking for because it will not allow integer
> specific matches.
> For instance (not normative), the regular expression syntax and
> semantics would allow complex matches such as "0%30" === "this integer
> matches if and only if its modulo to 30 is equal to 0". As in string
> regular expressions, operator | will implement a logical or...
> I googled a little but all the results are always related to string
> regular expressions :( any pointers? any existing Caml code?

You want something called "views" or "active patterns".
You can do that in OCaml with mikmatch, which includes such syntax extension.


Here is your example:

let view Mod30 = fun x -> x mod 30 = 0
   but not:
     let view Mod m = fun x -> x mod m = 0

   but it could be implemented without difficulty (if really needed).

let test x =
  match x with
     %Mod30 -> ...
   | ... ->

Of course the whole point is to use %Mod30 within arbitrary patterns,
otherwise it wouldn't be useful.

I you want to match regular expressions over anything else than bytes, there's
nothing out-of-the-box. You can define views on lists that would consume any
number of elements, but it is pretty limited.