R
RichardOnRails
Hi All,
I want to extract numbers from records with a leading string of period-
separated numbers. I got great responses to this on the thread
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/a811f41d733125f3#,
including the program below (stripped of all error handling).
My question is the meaning of a couple of constructs in the regular
expressions (and where I can find on-line documentation for them, if
possible):
1. "(.*?)", or specifically, the "?" in that expression.
2. "(?:\d+ [.]?)", or the two question marks in this case.
Thanks in advance,
Richard
Program
=======
input = <<DATA
2.002.1Topic 2.2.1
2.1Topic 2.1
2.2.02Topic 2.2.2
DATA
input.each do |line|
puts "\n" + "="*10 + "DBG", line, "="*10+ "DBG\n"
if line =~ /^ (.*?) [a-zA-Z] /x # Question 1
prefix = $1
if prefix =~ /^ (?:\d+ [.]?)+ $ /x # Question 2
arr = prefix.split('.')
print " Numbers: ", arr.join(', '), "\n"
end
end
end # input
I want to extract numbers from records with a leading string of period-
separated numbers. I got great responses to this on the thread
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/a811f41d733125f3#,
including the program below (stripped of all error handling).
My question is the meaning of a couple of constructs in the regular
expressions (and where I can find on-line documentation for them, if
possible):
1. "(.*?)", or specifically, the "?" in that expression.
2. "(?:\d+ [.]?)", or the two question marks in this case.
Thanks in advance,
Richard
Program
=======
input = <<DATA
2.002.1Topic 2.2.1
2.1Topic 2.1
2.2.02Topic 2.2.2
DATA
input.each do |line|
puts "\n" + "="*10 + "DBG", line, "="*10+ "DBG\n"
if line =~ /^ (.*?) [a-zA-Z] /x # Question 1
prefix = $1
if prefix =~ /^ (?:\d+ [.]?)+ $ /x # Question 2
arr = prefix.split('.')
print " Numbers: ", arr.join(', '), "\n"
end
end
end # input