Hi –
And at what point would you be
matching something? Could you show a mock-up example of a whole real
case?Now you’re asking me to invent syntax on the spur of
the moment. I’m not good at that.Here’s a very rough first effort. (One too simple really.)
phone = RegexLang.new(<<EOF)
string “(”
digits(3,:area_code,Fixnum)
# Above: Grab three digits, store in area_code
# as a Fixnum
string ") "
match(:rest) do # Store this stuff in ‘rest’ as
digits(3) # a String
string “-”
digits(4)
end
EOFarea_code = rest = nil
str = “(800) 555-1234”
phone.match(str)
puts area_code # 800
puts rest # 555-1234
area_code.is_a? Fixnum # true
puts phone.to_r # /((\d{3}) (\d{3}-\d{4})/There are lots of problems here. I just tossed it
off the top of my head.
Hmmm… I guess readability is in the eye of the beholder. Give me
/((\d{3})) (\d{3})-(\d{4})/ any day Or
/
Area code: ‘(’ + 3 digits + ') ’
\((\d{3})\)\
Number: 3 digits + ‘-’ + 4 digits
(\d{3})-(\d{4})
/x
though I find the first one much clearer.
The thing with the local variables is kind of unexpected. Might it be
more idiomatic to have an object with named attributes?
res = phone.match(str)
puts res.area_code # etc.
David
···
On Thu, 17 Apr 2003, Hal E. Fulton wrote:
–
David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav