M
Martin Elzen
Hi everyone.
I'm trying to learn how to program in Ruby, and yesterday I came across a
Regexp issue I can't figure out. I want to parse a commandline argument of
the form: max_repeats=<integer>
After studying the 'pick-axe' book, I came up with the line:
regexp = Regexp.new("\Amax_repeats=(\d+)")
but the above regexp fails to match with correct input. When I changed that
line to the following:
regexp = Regexp.new("^max_repeats=([0-9]+)")
it *does* match correctly with the proper input.
What I would like to know is, why doesn't the first Regexp work? (FWIW, I'm
using Ruby 1.8.1 on Win2003 server.)
Sincerely,
Martin
PS a complete program with my regexp matching code is as follows:
#define globals
$max_repeat_count_default = 5
def get_max_repeats_value
result = $max_repeat_count_default
if not ARGV.empty?
#regexp = Regexp.new("\Amax_repeats=(\d+)")
#@@@
regexp = Regexp.new("^max_repeats=([0-9]+)")
matchdata = candidate_val = match_val = nil
ARGV.each do |arg|
matchdata = regexp.match(arg)
if matchdata != nil
match_val = matchdata[1].to_i
if candidate_val == nil
candidate_val = match_val
else
candidate_val = match_val if match_val > candidate_val
end
end
end
result = candidate_val if candidate_val != nil and candidate_val > 0
end
result
end
begin
max_repeats = get_max_repeats_value
puts "max_repeats=" + max_repeats.to_s
rescue Exception => ex
print "Exception exception: " + ex.message +
",\nbacktrace:\n" + ex.backtrace.join("\n")
end
_________________________________________________________________
Hotmail en Messenger on the move
http://www.msn.nl/communicatie/smsdiensten/hotmailsmsv2/
I'm trying to learn how to program in Ruby, and yesterday I came across a
Regexp issue I can't figure out. I want to parse a commandline argument of
the form: max_repeats=<integer>
After studying the 'pick-axe' book, I came up with the line:
regexp = Regexp.new("\Amax_repeats=(\d+)")
but the above regexp fails to match with correct input. When I changed that
line to the following:
regexp = Regexp.new("^max_repeats=([0-9]+)")
it *does* match correctly with the proper input.
What I would like to know is, why doesn't the first Regexp work? (FWIW, I'm
using Ruby 1.8.1 on Win2003 server.)
Sincerely,
Martin
PS a complete program with my regexp matching code is as follows:
#define globals
$max_repeat_count_default = 5
def get_max_repeats_value
result = $max_repeat_count_default
if not ARGV.empty?
#regexp = Regexp.new("\Amax_repeats=(\d+)")
#@@@
regexp = Regexp.new("^max_repeats=([0-9]+)")
matchdata = candidate_val = match_val = nil
ARGV.each do |arg|
matchdata = regexp.match(arg)
if matchdata != nil
match_val = matchdata[1].to_i
if candidate_val == nil
candidate_val = match_val
else
candidate_val = match_val if match_val > candidate_val
end
end
end
result = candidate_val if candidate_val != nil and candidate_val > 0
end
result
end
begin
max_repeats = get_max_repeats_value
puts "max_repeats=" + max_repeats.to_s
rescue Exception => ex
print "Exception exception: " + ex.message +
",\nbacktrace:\n" + ex.backtrace.join("\n")
end
_________________________________________________________________
Hotmail en Messenger on the move
http://www.msn.nl/communicatie/smsdiensten/hotmailsmsv2/