On Mon, May 5, 2008 at 11:51 AM, Andreas Launila <ruby-talk / lokorin.org> wrote:
> Robert Klemme wrote:
>  > 2008/5/5 Andreas Launila <ruby-talk / lokorin.org>:
>  >> I'm trying to come up with a clean way to specify regexps in the integer
>  >>  domain. I.e. instead of describing a pattern of characters (as in normal
>  >>  regexps) one describes patterns of integers ("17 followed by 3 or 15"
>  >>  rather than "'a' followed by 'b' or 'c'").
>  >
>  > Why do you need this, i.e. what advantages do you expect for a
>  > particular "integer regexp" over classic regular expressions?
>  >
>
>  The two types of regexps are not really comparable since they work in
>  different domains. I.e. classic regular expressions describe patterns in
>  arrays of characters rather than in arrays of integers. One could for
>  instance use an integer regexp to decide whether an array begins with 17
>  and ends with 8.
>
>  Specifically this is for specifying deterministic finite automatons,
>  with an integer alphabet, for an upcoming constraint in Gecode/R[1].

I'd implement this using a generic repeat operator instead of
hardcoding * and +, and using a generic comparator instead of
restricting to Integers.

The declaration for this would be something like

# Use repeat(0, nil, ...) for *
# Use repeat(1, nil, ...) for +
# Use repeat(0, 1, ...) for ?
def repeat(minimum, maximum, repeated_expression)
def or(*expressions)

So you'd convert your example "(17|1 5)*" to repeat(0, nil, or(17, [1, 5]))

Actually, as I thought of after I had written the above, I have
written code that does part of this already, and is generic - it's
available in types.rb (http://people.freebsd.org/~eivind/ruby/types/).
 If you want it, I think I can extend that to support repeats tonight.
 Feel free to use code from it under any license that absolves me of
legal blame (ie, the only reason I don't put it in the public domain
is because that expose me to legal liability.)

Eivind.