From: "ara.t.howard" Date: 2008-05-06T01:48:00+09:00 Subject: Re: How would you design regexps in the integer domain? On May 5, 2008, at 2:24 AM, Andreas Launila wrote: > 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'"). > > My main objective is to make the syntax intuitive to Ruby users. I > have > been toying around with a few different approaches, but I'm not sure > if > any meet the goal. How would you design the syntax for regexps in the > integer domain? > > The syntax does not need to support especially many operators: > * Kleene star ('*' in character regexps) > * "At least once" ('+' in character regexps) > * Alternation ('|' in character regexps, i.e. "a|b" being "'a' or > 'b'") > > Below are some of the approaches one could take. i personally would not bother making a new syntax, rather i'd simply use the exiting one and add a new atom builder: cfp:~ > cat a.rb N = 0.chr def N n "#{ n }#{ N }" end module Enumerable def nmatch pattern match = pattern.match( select{|i| Fixnum === i}.join(N) ) if match returned = [] captured = match.to_a.map{|capture| capture.split N} returned << captured.first.map{|s| Float(s).to_i} captured[1..-1].each do |list| returned << Float(list.first).to_i end returned else nil end end end pattern = %r/#{ N 42 }#{ N 7 }(#{ N 17 }){2}(#{ N 1 }){1,}/ p [42, 7, 17, 17, 1, 1, 1].nmatch( pattern ).to_a cfp:~ > ruby a.rb [[42, 7, 17, 17, 1, 1], 17, 1] a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama